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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Aug 2006
    Location
    Canada
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Testing HTML form for numeric values

    Hi everyone:

    Just wondering if anyone can help me with this. I've created an HTML form and written a javascript that tests to make sure users don't leave any fields blank when submitting the form. One of the fields on the form is the phone_number. I want to make sure that users only enter the numeric value in this field. If for example, they enter some letters in the phone_number field then javascript should prompt them to only enter the numbers. How would I do this? Here's a code I am using to make sure fields are NOT left empty:

    if (formObj.phone_number.value == "") {
    window.alert("Please enter a valid phone number along with the area code");
    formObj.phone_number.focus();
    return false;
    }


    Thanks for your help and cooperation in advance.

  • #2
    New Coder
    Join Date
    Apr 2007
    Location
    Silicon Valley California
    Posts
    71
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Use a regex to test if it is a valid phone #.

    Code:
    var regex = /^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,5})|(\(?\d{2,6}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$/;
    regex .test(val);
    this will except mode common phone formats and international phone formats. erase any phone # that doesn't work and tell your user to use a format like "999-888-7777"

  • #3
    Regular Coder
    Join Date
    Oct 2003
    Location
    on a ship
    Posts
    574
    Thanks
    1
    Thanked 6 Times in 5 Posts
    can also use isNaN.

    example:


    if(isNaN(element.value)){alert('the value is not a number');}
    I make no attempt at pretending like I'm a professional. I offer help with what knowledge I do have.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,029
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    As with most other validations, over-elaboration of telephone number validation can be counter-productive. My UK (ex-directory) phone number is something like 01485 765387 (not normally with a hyphen). Cyberlogi's regex does not accept this.

    The main risk is that the user simply enters the wrong number (transposed digits etc.) and nothing can prevent this. In fact I often deliberately enter a wrong number where the field is compulsory to eliminate unwanted spam calls.

    I would advise that telephone number validation is simply a check that the input box contains only numbers (without specifying how many or their precise format which varies from country to country), and also + (international prefix), - (hyphens) and/or spaces.

    - and space can be stripped out if necessary.

    The regex is:-

    if (/[^0-9\s\-\+]/gi.test(phoneNumber.value) || (phoneNumber.value.length <9 )) { /// only digits, space, hyphen and plus sign allowed. Minimum of 9 characters.

    You can test your regular expressions at: http://www.ogauge.co.uk/regextester.html
    Last edited by Philip M; 06-07-2007 at 07:42 AM.


  •  

    Posting Permissions

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