Javascript 如何在javascript中验证密码、手机号码和电子邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28716539/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to validate pin code, mobile number and email in javascript
提问by Dipika Awasthi
here is my code and it's not working in my form.
这是我的代码,它在我的表单中不起作用。
<script type="text/javascript">
function valid(){
var pin_code=document.getElementById("pin");
var user_mobile=document.getElementById("phone");
var user_id=document.getElementById("email");
var pat1=/^([0-9](6,6)+$/;
var pattern=/^([0-9](10,10))+$/;
var filter=/^([a-z A-Z 0-9 _\.\-])+\@(([a-z A-Z 0-9\-])+\.)+([a-z A-z 0-9]{3,3})+$/;
if (!filter.test(user_id.vlaue)) {
alert("Email is in www.gmail.com format");
user_id.focus();
return false;
}
if (!pattern.test(user_mobile.value)) {
alert("Phone nubmer is in 0123456789 format ");
user_mobile.focus();
return false;
}
if (!pat1.test(pin_code.value)) {
alert("Pin code should be 6 digits ");
pin_code.focus();
return false;
}
}
</script>
Here is the issue, when i submit the form whether i enter digits or characters in the mobile number or pin code it's accepting that value also. and when i am using these codes in partitions means like for email
这是问题,当我提交表单时,无论我在手机号码或密码中输入数字还是字符,它也接受该值。当我在分区中使用这些代码时,就像电子邮件一样
<script type="text/javascript">
function valid() {
var user_id=document.getElementById("email");
var filter=/^([a-z A-Z 0-9 _\.\-])+\@(([a-z A-Z 0-9\-])+\.)+([a-z A-z 0-9 {3,3})+$/;
if(!filter.test(user_id.vlaue)) {
alert("Email is in www.gmail.com format");
user_id.focus();
return false;
}
}
</script>
in this code it's working properly but it's not working when i am using all the codes in one single form.
在这段代码中,它工作正常,但是当我以一种形式使用所有代码时它不起作用。
Please help me. Thank You.
请帮我。谢谢你。
采纳答案by Amogh
Just change pat1and patternto this:
只需更改pat1并更改pattern为:
var pat1=/^\d{6}$/;
var pattern=/^\d{10}$/;
Full working java-script as follows:
完整的工作java脚本如下:
<script type="text/javascript">
function valid()
{
var pin_code=document.getElementById("pin");
var user_mobile=document.getElementById("phone");
var user_id=document.getElementById("email");
var pat1=/^\d{6}$/;
var pattern=/^\d{10}$/;
var filter=/^([a-z A-Z 0-9 _\.\-])+\@(([a-z A-Z 0-9\-])+\.)+([a-z A-z 0-9]{3,3})+$/;
if(!filter.test(user_id.value))
{
alert("Email is in www.gmail.com format");
user_id.focus();
return false;
}
if(!pattern.test(user_mobile.value))
{
alert("Phone nubmer is in 0123456789 format ");
user_mobile.focus();
return false;
}
if(!pat1.test(pin_code.value))
{
alert("Pin code should be 6 digits ");
pin_code.focus();
return false;
}
}
</script>
回答by varad mayee
change the pattern for the regular expression as below:
更改正则表达式的模式如下:
var pat1=/^[0-9]{1,6}$/;
var pattern=/^[0-9]{1,10}$/;
回答by shipra shrivastav
app.directive('validateAddress', function() {
return {
require: 'ngModel',
restrict: 'A',
link: function(scope, element, attrs, modelCtrl) {
modelCtrl.$parsers.push(function(inputValue) {
if (inputValue == null)
return ''
//cleanInputValue = inputValue.replace(/[^\w]/gi, '');
//cleanInputValue = inputValue.replace(/[^a-zA-Z0-9-\,\s\_\.\@\#]/g, "");
cleanInputValue = inputValue.replace(/[^a-zA-Z0-9-\,\s\_\.\@\#\!$\%\*\(\)\-\+\;\:\>\<\?\|\}\{\=]/g, "");
if (cleanInputValue != inputValue) {
modelCtrl.$setViewValue(cleanInputValue);
modelCtrl.$render();
}
return cleanInputValue;
});
}
}
})

