Click here to Skip to main content
14,920,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
phone number validation code in javascript
Posted
Comments
Pete O'Hanlon 24-Apr-12 6:14am
   
Is this a question or a statement? Plus, phone number validation in what country? Different countries have different phone number standards. You really need to define your requirements more - and that will help you come up with a solution.

1 solution

C#
<script type="text/javascript">

function validateFormOnSubmit(theForm) {
var reason = "";

  reason += validateUsername(theForm.formtext1);
  reason += validatePassword(theForm.formtext2);
  reason += validateEmail(theForm.formtext5);
  reason += validatePhone(theForm.formtext4);
  reason += validatePasswordMissmatch(theForm.formtext2,theForm.formtext3);
  //reason += validateEmpty(theForm.from);

  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}

function validatePasswordMissmatch(fld1,fld2)
{
    var error = "";
    if(fld1.value!=fld2.value)
    {
        fld1.style.background = 'Yellow';
        fld2.style.background = 'Yellow';
        error = "Password Missmatch.\n";
    }
    return error;
}


function validateEmpty(fld) {
    var error = "";

    if (fld.value.length == 0) {
        fld.style.background = 'Yellow';
        error = "The required field has not been filled in.\n";
        fld.vlaue="";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateUsername(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores

    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "You didn't enter a username.\n";

    } else if ((fld.value.length < 5) || (fld.value.length > 15)) {
        fld.style.background = 'Yellow';
        fld.vlaue="";
        error = "The username is the wrong length.\n";


    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow';
        fld.vlaue="";
        error = "The username contains illegal characters.\n";

    } else {
        fld.style.background = 'White';
    }
    return error;
}
function validatePassword(fld) {
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers

    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "You didn't enter a password.\n";
    } else if ((fld.value.length < 6) || (fld.value.length > 14)) {
        error = "The password is the wrong length. \n";
        fld.vlaue="";
        fld.style.background = 'Yellow';

    } else if (illegalChars.test(fld.value)) {
        error = "The password contains illegal characters.\n";
        fld.vlaue="";
        fld.style.background = 'Yellow';

    } else if (!((fld.value.search(/(a-z)+/)) && (fld.value.search(/(0-9)+/)))) {
        error = "The password must contain at least one numeral.\n";
        fld.vlaue="";
        fld.style.background = 'Yellow';

    } else {
        fld.style.background = 'White';
    }
   return error;
}

function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}

function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;

    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "You didn't enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        fld.vlaue="";
        error = "Please enter a valid email address.\n";

    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        fld.vlaue="";
        error = "The email address contains illegal characters.\n";

    } else {
        fld.style.background = 'White';
    }
    return error;
}
function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');

   if (fld.value == "") {
        error = "You didn't enter a phone number.\n";
        fld.style.background = 'Yellow';
    } else if (isNaN(parseInt(stripped))) {
        fld.vlaue="";
        error = "The phone number contains illegal characters.\n";
        fld.style.background = 'Yellow';

    } else if (!(stripped.length == 10)) {
        fld.vlaue="";
        error = "The phone number is the wrong length. Make sure you included an area code.\n";
        fld.style.background = 'Yellow';

    }
    return error;
}


</script>







XML
<form name="form11" action="Register.jsp" method="get" style="margin:0px" onSubmit="return validateFormOnSubmit(this)">
  <input name="formtext1" type="text" style="position:absolute;width:200px;left:519px;top:299px;z-index:9"  onFocus="showCriteria1()">
  <input name="formtext2" type="password" style="position:absolute;width:200px;left:519px;top:330px;z-index:10" onFocus="showCriteria2()">
  <input name="formtext3" type="password" style="position:absolute;width:200px;left:519px;top:370px;z-index:11" onFocus="showCriteria3()">
  <textarea name="textarea1" style="position:absolute;left:519px;top:403px;width:200px;height:90px;z-index:12" onFocus="showCriteria5()"></textarea>
  <input name="formtext4" type="text" style="position:absolute;width:200px;left:519px;top:511px;z-index:13" onFocus="showCriteria4()">
  <input name="formtext5" type="text" style="position:absolute;width:200px;left:519px;top:556px;z-index:14" onFocus="showCriteria5()">
  <input name="formbutton1" type="submit" style="position:absolute; left:545px; top:605px; z-index:15; width: 100;" value="Register" >
  <input name="formbutton2" type="reset" style="position:absolute; left:417px; top:606px; z-index:16; width: 100;" value="Clear  ">
</form>
   
Comments
Pete O'Hanlon 24-Apr-12 6:39am
   
A reasonable example, but there are assumptions in there that might not be valid. For instance, you are checking that the phone number is 10 characters, well mine is 11 characters. With country code, it's 14.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900