#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2001
    Location
    Israel
    Posts
    41
    Rep Power
    13

    regexp newbi prob...


    well i guess for u javascript pro's this is too simple...
    the thing is this: i have an input type text in my form.
    i need a way to verifay that the data in that text filed is just numbers.
    Using the isNaN, will work but i need to use regExp....
    now, i tried to do something like this:

    if (document.myForm.Number.value.search(/[^0-9]) != -1)...

    now in this way i get -1 if the value is just numbers. any other cobination of letters or numbers and letters will give a result different than -1.
    now, this is just fine exept for one case where the value is empty. in this case i get the result of -1
    so insted of having -1 only in case the value is numbers (and only numbers) i get the -1 result in two cases, one, when the value is numbers and second when the value is empty...
    in order to solve this i currently check the value to see if its empty, but that makes some of the conditions in my script waaay too long and complicated...
    there should be a way to create a regexp that will give one result when the value is only number and a different result in any other case (including a case where the value is empty).
    any one??

    thnx in advanced.
    Shanor.
    You cant see yourself in the mirror with your eyes closed.
  2. #2
  3. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    Well, first of all you are posting in the wrong forum, but I will answer your question anyway. Instead of using
    Code:
    if (document.myForm.Number.value.search(/[^0-9]) != -1)...
    try using
    Code:
    if (document.myForm.Number.value.search(/^[0-9]+$/) != -1))
    That regex should tell you if any characters in the string are anything other than 0 thru 9, including an empty string I would think. If anything in the string is other than 0-9 it will return -1. I did not test this, but it *should* work.

IMN logo majestic logo threadwatch logo seochat tools logo