SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Member
    Join Date
    Aug 2010
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Phone field validation

    Hello,

    I don’t know the javascript well.
    I have forum validation script but this script can’t validate the phone number field. My script:

    if(document.getElementById('user_phone').value=='')
    {
    alert(Please, enter <?php _e(PHONE_TEXT) ?>');
    document.getElementById('user_phone').focus();
    return false;
    }

    This script need to recompose that it validates entering data. I found validation script and tried to recompose this script but didn’t success. Who can help me?

    function checkPhone (strng) {
    var error = "";
    if (strng == "") {
    error = "You didn't enter a phone number.\n";
    }

    var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
    if (isNaN(parseInt(stripped))) {
    error = "The phone number contains illegal characters.";

    }
    if (!(stripped.length == 10)) {
    error = "The phone number is the wrong length. Make sure you included an area code.\n";
    }
    return error;
    }

  2. #2
    SitePoint Zealot
    Join Date
    May 2009
    Location
    Netherlands
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does this help:
    Code:
    function validate() {
    var phone = document.getElementById('user_phone').value;
    phone = phone.replace(/[ \D]+/g,'');
    if(phone.length!=10 || phone.charAt(0)!=0) { // 10 digits, fist a zero
        alert('incorrect phone number');
        phone.focus();
        return false;
        }
    else {
        document.getElementById('user_phone').value = phone;
        }
    }

  3. #3
    SitePoint Member
    Join Date
    Aug 2010
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you. I wrote “ggggg” in this field but the script didn’t noticed this.

  4. #4
    SitePoint Zealot
    Join Date
    May 2009
    Location
    Netherlands
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How have you implemented the function in your document? Anything except a valid number will get caught.

  5. #5
    SitePoint Member
    Join Date
    Aug 2010
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My form validation script start like this function “function chk_form_reg()”. After this goes:

    {
    if(document.getElementById('user_login1reg').value == '')
    {
    alert("Please, enter <?php _e(USERNAME_TEXT) ?>");
    document.getElementById('user_login1reg').focus();
    return false;
    }
    I wrote your script below:

    function validate() {
    var phone = document.getElementById('user_phone').value;
    phone = phone.replace(/[ \D]+/g,'');
    if(phone.length!=10 || phone.charAt(0)!=0) { // 10 digits, fist a zero
    alert('incorrect phone number');
    phone.focus();
    return false;
    }
    else {
    document.getElementById('user_phone').value = phone;
    }
    }

  6. #6
    SitePoint Zealot
    Join Date
    May 2009
    Location
    Netherlands
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
        "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title></title>
    
    <script type="text/javascript">
    window.onload=function() {
    var aObj=document.getElementsByTagName('form');
    aObj[0].onsubmit=function() {return validate(this);};
    };
    
    function validate() {
    if(document.getElementById('user_login1reg').value == '')
    {
    alert("Please, enter <?php _e(USERNAME_TEXT) ?>");
    document.getElementById('user_login1reg').focus();
    return false;
    }
    
    var phone = document.getElementById('user_phone').value;
    phone = phone.replace(/[ \D]+/g,'');
    if(phone.length!=10 || phone.charAt(0)!=0) { // 10 digits, fist a zero
        alert('incorrect phone number');
        document.getElementById('user_phone').focus();
        return false;
        }
    else {
        document.getElementById('user_phone').value = phone;
        }
    }
    </script>
    
    <style type="text/css">
    * {margin:0;padding:0;}
    </style>
    
    </head>
    <body>
    <form action="#" method="" name="form1">
    <div>
    <label>login: <input type="text" id="user_login1reg" name="user_login1reg"></label>
    <label>phone: <input type="text" id="user_phone" name="user_phone"></label>
    <button type="submit">submit</button>
    </div>
    </form>
    </body>
    </html>

  7. #7
    SitePoint Member
    Join Date
    Aug 2010
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dear friend,please,look at attached file.
    Attached Files Attached Files

  8. #8
    SitePoint Zealot
    Join Date
    May 2009
    Location
    Netherlands
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Replace the existing phone validation with:
    Code:
    var phone = document.getElementById('user_phone').value;
    phone = phone.replace(/[ \D]+/g,'');
    if(phone.length!=10 || phone.charAt(0)!=0) { // 10 digits, fist a zero
        alert('incorrect phone number');
        document.getElementById('user_phone').focus();
        return false;
        }
    else {
        document.getElementById('user_phone').value = phone;
        }

  9. #9
    SitePoint Member
    Join Date
    Aug 2010
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you of your help. I did such as you advice but the script didn't work. It isn't notice that field empty or entered bad data.
    The script:
    Code JavaScript:
    function validate() {
     
    		if(document.getElementById('user_phone').value=='')
    		{
    			alert('Please, enter <?php _e(PHONE_TEXT) ?>');
    			document.getElementById('user_phone').focus();
    			return false;
    		}
    	var phone = document.getElementById('user_phone').value;
    phone = phone.replace(/[ \D]+/g,'');
    if(phone.length!=10 || phone.charAt(0)!=0) { // 10 digits, fist a zero
        alert('incorrect phone number');
        document.getElementById('user_phone').focus();
        return false;
        }
    else {
        document.getElementById('user_phone').value = phone;
        }
    		}

  10. #10
    SitePoint Zealot
    Join Date
    May 2009
    Location
    Netherlands
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Replace your existing phone validation with the code given. Do not create another function.
    Replase this:
    Code:
    		if(document.getElementById('user_phone').value=='')
    		{
    			alert('&#208;Ÿ&#208;&#190;&#208;&#182;&#208;&#176;&#208;&#187;&#209;ƒ&#208;&#185;&#209;&#209;‚&#208;&#176;, &#208;&#178;&#208;&#178;&#208;&#181;&#208;&#180;&#208;&#184;&#209;‚&#208;&#181; <?php _e(PHONE_TEXT) ?>');
    			document.getElementById('user_phone').focus();
    			return false;
    		}
    with this:
    Code:
    var phone = document.getElementById('user_phone').value;
    phone = phone.replace(/[ \D]+/g,'');
    if(phone.length!=10 || phone.charAt(0)!=0) { // 10 digits, fist a zero
        alert('incorrect phone number');
        document.getElementById('user_phone').focus();
        return false;
        }
    else {
        document.getElementById('user_phone').value = phone;
        }

  11. #11
    SitePoint Member
    Join Date
    Aug 2010
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you. The script work good.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •