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 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    numeric characters only in form field

    Hi guys,

    This has been asked before, and i've seen many of the previous posts. And tried for hours. But finally i've broken and have to ask for help.

    What am I doing wrong here? Can someone please explain?

    Code:
    var u=document.forms["lead"]["name"].value;
    var v=document.forms["lead"]["city"].value;
    var x=document.forms["lead"]["country"].value;
    var y=document.forms["lead"]["mobile"].value;
    var z=document.forms["lead"]["email"].value;
    
    if (u==null || u=="")
      {
      alert("Please enter your first name.");
      return false;
      }
      
      if (v==null || v=="")
      {
      alert("Please enter your city.");
      return false;
      }
      
      if (isNaN(lead.y.value))
      { 
      alert("bloody work!");
      return false;
      }
      
      if (x=="Argentina" && y.length < 11)
      {
      alert("Please enter a valid Argentinian phone number.");
      return false;
      }
    Thanks in advance.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,200
    Thanks
    75
    Thanked 4,342 Times in 4,308 Posts
    Code:
    if (isNaN(y))
      { 
      alert("bloody work!");
      return false;
      }
    Though that is rather inflexible. Don't people in Argentina ever enter spaces or dashes in phone numbers? Such as 337-111 88821?? (Or various other formats.)

    I would do this:
    Code:
    var numonly = y.replace(/[^\d]/g, "" );
    if (x=="Argentina" && numonly.length < 11)
      {
      alert("Please enter a valid Argentinian phone number.");
      return false;
      }
    and forget the other test.
    Last edited by Old Pedant; 10-03-2011 at 08:49 PM.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,200
    Thanks
    75
    Thanked 4,342 Times in 4,308 Posts
    And it's minor, but you are working too hard.

    In place of
    Code:
    var u=document.forms["lead"]["name"].value;
    var v=document.forms["lead"]["city"].value;
    var x=document.forms["lead"]["country"].value;
    var y=document.forms["lead"]["mobile"].value;
    var z=document.forms["lead"]["email"].value;
    try simply
    Code:
    var form = document.lead;
    var u = form.name.value;
    var v = form.city.value;
    var x = form.country.value;
    var y = form.mobile.value;
    var z = form.email.value;
    though, personally, I wouldn't use names such as x,y,z,etc.

    What's wrong with
    Code:
    var form = document.lead;
    var name = form.name.value;
    var city = form.city.value;
    var country = form.country.value;
    var mobile = form.mobile.value;
    var email = form.email.value;
    ??? So that your code is self-documenting and self-explanatory?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks so much for your help.

    I'm keen to understand and learn the code. Do you mind explaining how this line works please?

    Code:
    var numonly = y.replace(/[^\d]/g, "" );

  • #5
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    This is the replace method whose first parameter is a so-called "regular expression". This is quite sophisticated stuff

    General explanation:
    ^\d means: non-digit characters
    /.../g means: globally

    So this regex matches all non-digit characters in the string y and replaces them with "" (so removes them)

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    One more time, form validation of the pattern if (u=="") 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. Numeric values, such as zip codes and phone numbers, should be validated as such. Ditto email addresses. This topic has been covered many times before in this forum.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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