[英]Javascript phone and number field wont validate correctly submits form anyway
Update: 更新:
the script below will throw an error if I enter in a 9 digit phone number, and accept a 10 digital one ...... but it will also accept just a single digit - how can I stop this from happening. 如果我输入9位数的电话号码并接受10位数的电话号码,以下脚本将引发错误……但是它也仅接受一个数字-我该如何阻止这种情况的发生。
and for the collector field I need it to accept only 11 numbers. 对于收集器字段,我只需要接受11个数字即可。
I'm trying to amend my validation code to validate for phone numbers, it seems like an easy enough task but I can't get it to work correctly. 我正在尝试修改验证码以验证电话号码,这似乎很容易,但是我无法使其正常工作。
The script should check to see if it is 9 digits long, spaces, dashes or no spaces are okay. 该脚本应检查它是否为9位数字长,空格,破折号或没有空格。 If no phone is entered it should give the required error.
如果未输入电话,则应给出所需的错误。 If the field has only 8 digits for example entered it should give the invalid phone error.
例如,如果该字段仅输入8位数字,则应给出无效的电话错误。
Please see the code at this jsfiddle - http://jsfiddle.net/5zFqS/7/ 请在此jsfiddle中查看代码-http: //jsfiddle.net/5zFqS/7/
function validate_required(field,alerttxt) {
with (field) {
if (value==null||value=="") {
alert(alerttxt);return false;
} else {return true;}
}
}
function validate_email(field,alerttxt) {
with (field) {
apos=value.indexOf("@");
dotpos=value.lastIndexOf(".");
if (apos<1||dotpos-apos<2)
{alert(alerttxt);return false;}
else {return true;}
}
}
function validate_Phone(field,alerttxt) {
var phoneno = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;
if(field.value.match(phoneno)) {
alert(alerttxt);return false;
} else {return true;}
}
function validate_collector(field,alerttxt) {
var collect = /^\d{12}$/;
if(field.value.match(collect)) {
alert(alerttxt);return false;
} else {return true;}
} }
function validate_form(thisform) {
with (thisform) {
if (validate_required(firstName,"Please enter your First Name")==false)
{firstName.focus();return false;}
if (validate_required(lastName,"Please enter your Last Name")==false)
{lastName.focus();return false;}
if (validate_required(email,"Please enter your Email Address")==false)
{email.focus();return false;}
if (validate_email(email,"Please enter a valid Email Address")==false)
{email.focus();return false;}
if (validate_required(phone,"Please enter your Phone")==false)
{phone.focus();return false;}
if (validate_Phone(phone,"Please enter a valid Phone Number")==false)
{phone.focus();return false;}
if (validate_required(province,"Please select your Province")==false)
{province.focus();return false;}
if (validate_required(collector,"Please enter Collector Number")==false)
{collector.focus();return false;}
if (validate_collector(collector,"Please enter a valid Collector Number")==false)
{collector.focus();return false;}
}
}
I think I have a syntax error but I can't see it. 我认为我有语法错误,但看不到。
You need to remove the semi-colon at the end of this line: 您需要在此行的末尾删除分号:
if (field.match(/^\d{9}/));
You said that spaces etc., should be okay. 您说空格等应该没问题。 In which case, you'll need to remove (or ignore) them:
在这种情况下,您需要删除(或忽略)它们:
var reg = /\D/g; // \D identifies non-digit characters, g means 'global'
var stripped = "888-777 66st".replace(reg,"");
// returns: 88877766
Also, use of with is not recommended 另外,不建议使用with
as it may be the source of confusing bugs and compatibility issues
因为它可能是引起混淆的错误和兼容性问题的根源
Instead of 代替
if (field.match(/^\d{9}/))
use this 用这个
if (!field.match(/\d{9}/))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.