简体   繁体   中英

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.

and for the collector field I need it to accept only 11 numbers.

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. 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.

Please see the code at this 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

as it may be the source of confusing bugs and compatibility issues

MDN reference

Instead of

if (field.match(/^\d{9}/))

use this

if (!field.match(/\d{9}/))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM