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 5 of 5
  1. #1
    New to the CF scene
    Join Date
    May 2009
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Very Very simple email and phone number validation

    I am in need of some very simple javascript to validate email address and a phone number from a form.

    The email address only needs to:
    1. Check that the box is not empty.
    2. Check that the text imputted contains an '@' symbol.
    3. Check that the text imputted contains atleast 1 Period.

    The phone number only needs to:
    1. Check that the box is not empty.
    2. Check that there is no more than 11 characters.
    3. check that only numeric characters have been inputted.

    I would like it to report (failure) back as text on the page under the box that has the problem rather than a window.

    Thanks,
    Mike

  • #2
    bdl
    bdl is offline
    Regular Coder
    Join Date
    Apr 2007
    Location
    Camarillo, CA US
    Posts
    590
    Thanks
    4
    Thanked 83 Times in 82 Posts
    I would suggest posting in the "Web Projects" forum, as you're requesting to have some code done for you as opposed to asking for help on your own code.

  • #3
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,026
    Thanks
    36
    Thanked 494 Times in 488 Posts

    Lightbulb

    One time request fulfilled. Still is a better idea to show some attempt.
    Code:
    <html>
    <head>
    <title>Email - Phone validation</title>
    <script type="text/javascript">
    // For: http://www.codingforums.com/showthread.php?t=166541
    
    function CheckEmail(info) {
      var flag = true;
      var obj = document.getElementById('divEmail');
      var msg = '';
      if (info.indexOf("@") == -1) { msg = "Email missing '@' character"; flag = false; }
      if (info.indexOf(".") == -1) { msg = "Email missing '.' character"; flag = false; }
      obj.innerHTML = msg;
      return flag;
    }
    function CheckPhone(info) {
      var flag = true;
      var obj = document.getElementById('divPhone');
      obj.innerHTML = '';
      if (info.length < 11) { obj.innerHTML = "Phone is less than 11 characters"; flag = false; }
      return flag;
    }
    function Validate() {
      flag = true;
      if ((CheckEmail(document.getElementById('email').value) == false)
       || (CheckPhone(document.getElementById('phone').value) == false)) { flag = false; }
      return flag;
    }
    function isNumberKey(evt) {
      var charCode = (evt.which) ? evt.which : event.keyCode
      if (charCode > 31 && (charCode < 48 || charCode > 57)) { return false; }
      return true;
    }
    
    </script>
    </head>
    <body>
    <h2>Email-Phone validation only<h2><p>
    Email: <input type="text" id="email" value=""><br>
    <div id="divEmail"></div>
    Phone: <input type="text" id="phone" value="" onkeypress="return isNumberKey(event)"><br>
    <div id="divPhone"></div>
    
    <!-- assume will be incorporated into some sort of <form> at a later time -->
    <br><button onClick="if (Validate() == true) { alert('Success'); } else {alert('Failure'); }">Validate</button>
    </body>
    </html>

  • Users who have thanked jmrker for this post:

    brodjah (05-17-2009)

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    The email address validation is pretty basic. Anything with an @ and a dot will pass. Even xx@.@.xx@

    A much better validation is

    Code:
    if (!(/^([a-z0-9])([\w\.\-\+])+([a-z0-9])\@(([\w\-]?)+\.)+([a-z]{2,4})$/i.test(eMail.value))) {
    alert ("Invalid email address!");
    return false;
    }

  • Users who have thanked Philip M for this post:

    brodjah (05-17-2009)

  • #5
    New to the CF scene
    Join Date
    May 2009
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Awesome, thankyou guys


  •  

    Posting Permissions

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