Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Jun 2010
    Posts
    23
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Post Phone Number Validation

    Need Phone Number Validation for my JavaScript, i can't work it out It currently has E-mail, Surname, Address and Name validation, This is my code at the moment:

    Code:
    <script language="JavaScript">
    function echeck(str) {
    
    		var at="@"
    		var dot="."
    		var lat=str.indexOf(at)
    		var lstr=str.length
    		var ldot=str.indexOf(dot)
    		if (str.indexOf(at)==-1){
    		   alert("Please Enter a Valid E-mail")
    		   return false
    		}
    
    		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
    		   alert("Please Enter a Valid E-mail")
    		   return false
    		}
    
    		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
    		    alert("Please Enter a Valid E-mail")
    		    return false
    		}
    
    		 if (str.indexOf(at,(lat+1))!=-1){
    		    alert("Please Enter a Valid E-mail")
    		    return false
    		 }
    
    		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
    		    alert("Please Enter a Valid E-mail")
    		    return false
    		 }
    
    		 if (str.indexOf(dot,(lat+2))==-1){
    		    alert("Please Enter a Valid E-mail")
    		    return false
    		 }
    		
    		 if (str.indexOf(" ")!=-1){
    		    alert("Please Enter a Valid E-mail")
    		    return false
    		 }
    		 
    
     		 return true					
    	}
    
    function validate_required(field,alerttxt)
    {
    with (field)
      {
      if (value==null||value=="")
        {
        alert(alerttxt);return false;
        }
      else
        {
        return true;
        }
      }
    }
    function validate_form(thisform)
    {
    with (thisform)
      {
      if (validate_required(Name,"Name must be filled out!")==false)
      {Name.focus();return false;}
      if (validate_required(Surname,"Surname must be filled out!")==false)
      {Surname.focus();return false;} 
      if (validate_required(Country,"Address must be filled out!")==false)
      {Country.focus();return false;}
      } 
      
      {
    	var emailID=document.submitting.email
    	
    	if ((emailID.value==null)||(emailID.value=="")){
    		alert("Please Enter a Valid E-mail")
    		emailID.focus()
    		return false
    	}
    	if (echeck(emailID.value)==false){
    		emailID.value=""
    		emailID.focus()
    		return false
    	}
    	return true
     }
    }
    
    function verifyEmail(form) {
    checkEmail = form.email.value
    
    if ((checkEmail.indexOf('@') < 0) || ((checkEmail.charAt(checkEmail.length-4) != '.') && (checkEmail.charAt(checkEmail.length-3) != '.')))
    {alert("You have entered an invalid email address. Please try again.");
    form.email.select();
    return false;
    }
    
    else {
    form.method="get";
    form.target="_self";
    form.action="myscript.cgi";
    form.submit();
    }
    }
    </script>
    Code:
            <form action="submit.htm" onsubmit="return validate_form(this)" method="post" name="submitting">
            <span class="style5">Select Plant:
            <form id="form1" name="form1" method="post" action="">
      <label>
                <select name="Item" id="Item">
                  <option>OMGwoopwoop Plant</option>
                  <option>I'm Blue Plant </option>
                  <option>Anonymous Plant</option>
                  <option>Rawr Plant</option>
                  <option>Chicka Plant </option>
                  <option>Fruitopola Plant</option>
                  <option>Whitoe Plant</option>
                  <option>Wukidoo Plant </option>
    </select>
              </label>
            </p>
          <p>Name:<span class="style1"> 
            <input type="text" name="Name" size="30">
            <br>
            <br>
                    </span>Surname:
            <span class="style1">
            <input type="text" name="Surname" size="30">
            <br>
            <br>
            </span>Address:<span class="style1">
             <input type="text" name="Country" size="30">
                            <br>
                            <br>
    		</span>Email:<span class="style1">
            <input type="text" name="email" size="30">
                                    <br></span></p>
            Phone Number:<span class="style1">
            <input type="text" name="phone" size="30">
          <form action="submit.htm" onSubmit="return validate_form(this)" method="post">
      <input type="submit" value="Submit">
    </form>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,314
    Thanks
    203
    Thanked 2,565 Times in 2,543 Posts
    This is ancient code. <script language=javascript> is long deprecated and obsolete. Use <script type = "text/javascript"> instead.

    Your email validation is absurdly complicated. Use a regular expression:-

    Code:
    if (!(/^([a-z0-9])([\w\.\-\+])+([a-z0-9])\@((\w)([\w\-]?)+\.)+([a-z]{2,4})$/i.test(email.value))) {   // Invalid email if returns true
    You do not say what the required phone number format is. This will cover most formats including international.

    Code:
    if (!(^(\+44)?[0-9\s\-]{10,})$/gi.test(phoneNumber.value)) { /// telephone number, only digits, space, hyphen and plus sign as first character are allowed. Minimum of 10 characters.  Invalid if returns true.
    Validation of the form if (value==null||value=="") is barely worthy of the name, and virtually useless, as even a single space, an x or a ? will return false, that is pass the validation. This topic has been covered many times before in this forum.

    filename.focus() does not work properly in Firefox.

    <form action="submit.htm" onsubmit="return validate_form(this)" method="post" name="submitting">
    <span class="style5">Select Plant:
    <form id="form1" name="form1" method="post" action="">
    You may not nest one form within another.

    Here is modern validation for first name, last name, phone number, email:-

    Code:
    <html>
    <head>
    <body>
    
    <form name="myForm" onSubmit="return checkForm();" action="processForm.php" method="post">
    
    <input type="text" size="40" name="firstName" value="*First Name" onfocus="this.value = ''" /> <br /> 
    <input type="text" size="40" name="lastName" value="Last Name" onfocus="this.value = ''" /> <br />
    <input type="text" size="40" name="phoneNumber" value="Phone" onfocus="this.value = ''" /> <br />
    <input type="text" size="40" name="emailAddress" value="*Email" onfocus="this.value = ''"  /> <br />
    <textarea rows="6" cols="30" onfocus="if (this.value =='Message') {this.value= ''}">Message</textarea>
    <input type="submit" value="Submit" />
    
    </form>
    <script type = "text/javascript">
    
    function checkForm(){
    var fn = document.myForm.firstName;
    fnval = fn.value;
    fnval = fnval.replace(/^\s+|\s+$/g,"");  // strip leading and trailing spaces
    if((fnval.length <2) || (/[^a-z\-\']/gi.test(fnval))) {  // hyphen and apostrophe may occur in name
    alert ("Please enter your first name using only letters a-z");
    document.myForm.firstName.value = "";
    myfield = fn;  // note myfield must be a global variable
    setTimeout('myfield.focus();myfield.select();' , 10);
    return false;
    }
    
    var ln = document.myForm.lastName;
    var lnval = ln.value;
    if (lnval != "Last Name") {
    lnval = lnval.replace(/^\s+|\s+$/g,"");  // strip leading and trailing spaces
    if((lnval.length <2) || (/[^a-z\-']/gi.test(lnval))) {  // hyphen and apostophe may occur in name
    alert ("Please enter your last name using only letters a-z, and hyphen/apostrophe if appropriate");
    document.myForm.lastName.value = "";
    myfield = ln;  // note myfield must be a global variable
    setTimeout('myfield.focus();myfield.select();' , 10);
    return false;
    }
    }
    
    var pn = document.myForm.phoneNumber;
    var pnval= pn.value;
    if (pnval !="Phone") {
    pnval = pnval.replace(/\s/g,"");  // strip all spaces
    if((pnval.length <10) || (/[^0-9\-]/gi.test(pnval))) {  // only digits and hyphen allowed
    alert ("Please enter your phone number - only digits and hyphen allowed");
    document.myForm.phoneNumber.value = "";
    myfield = pn;  // note myfield must be a global variable
    setTimeout('myfield.focus();myfield.select();' , 10);
    return false;
    }
    }
    
    var em = document.myForm.emailAddress;
    var emval = em.value;
    if (!(/^([a-z0-9])([\w\.\-\+])+([a-z0-9])\@(([\w\-]?)+\.)+([a-z]{2,4})$/i.test(emval))) {
    alert ("Invalid email address - please re-enter it");
    document.myForm.emailAddress.value = "";
    myfield = em;  // note myfield must be a global variable
    setTimeout('myfield.focus();myfield.select();' , 10);
    return false;
    }
    
    }
    
    </script>
    </body>
    </html>
    "In the beginner's mind there are many possibilities, but in the expert's mind there are few” - Shunryu Suzuki (Japanese Zen priest, ?-1971)
    Last edited by Philip M; 06-22-2010 at 01:34 PM.

  • Users who have thanked Philip M for this post:

    bubble00000 (06-22-2010)

  • #3
    Regular Coder
    Join Date
    Nov 2009
    Posts
    247
    Thanks
    4
    Thanked 22 Times in 22 Posts
    bubble:

    You may want to consider this:

    http://www.freewarefiles.com/Plug-n-...ram_53638.html

  • #4
    New Coder
    Join Date
    Jun 2010
    Posts
    23
    Thanks
    7
    Thanked 0 Times in 0 Posts
    With your code you said that your able to do first name, last name, phone number, email. It only validates First Name and E-mail, the JavaScript only validates Phone number or last name when you enter something in but if you were to press the submit button it would still work and not ask for you to enter anymore. What would be the solution to that?

    & thank you for you generous help so far!

  • #5
    Regular Coder
    Join Date
    Nov 2009
    Posts
    247
    Thanks
    4
    Thanked 22 Times in 22 Posts
    bubble:

    Did you see my previous post?

  • #6
    New Coder
    Join Date
    Jun 2010
    Posts
    23
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Yeah i saw your previous post, the problem i have with it is that its more experienced then what i am needing. but thank you for your contribution

  • #7
    New Coder
    Join Date
    Jun 2010
    Posts
    23
    Thanks
    7
    Thanked 0 Times in 0 Posts
    I got it working now
    I'd give a special thanks to Philip M cause i got the majority of my answers off Phone Number Validation


  •  

    Posting Permissions

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