SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    May 2006
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Number ONLY regular expression..

    I have a javascript function that I use to validate a Number Only field.

    I am not good with RegEx yet..

    I have a problem where the field allows 20+ to be entered and it crashes the Number only datatype on submit.

    How do I change it so the + symbol is removed? Also any character other than a digit? Cause they will enter 24k too for example, or 5mil

    Existing function:

    function numOnly(fld) {
    fld.style.backgroundColor="white";
    var reNum = /(^-?\d\d*$)/ig; // only digits allowed -- no text please
    var val = parseFloat(fld.value.replace(/[\s\,\\+\$]/g, "")); // remove spaces, commas and dollar signs
    if (!reNum.test(val)) {
    // alert("The value " + fld.value + " is not a valid number or currency for " + fld.name +".");
    fld.style.backgroundColor="#F60";
    fld.focus();
    var popHelp = window.open("/SBDX/Scripts/popHelp.asp?ID=" + fld.name + "&err=validation", "popErr", "resizable=1,height=300,width=400")
    popHelp.focus();
    val = null;
    }
    return val;
    }



    Help very appreciated.

  2. #2
    SitePoint Enthusiast nickweavers's Avatar
    Join Date
    Feb 2004
    Location
    Hampshire, UK
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My regexp skills are a little rusty but I think you can do the following...

    The following pattern will match if the string being tested against it contains only digits:

    var reNum = /^\d+$/ig; // only digits allowed

    As you probably already know, the \d is the meta character for a digit and the + sign means "at least one of and possibly more". The ^ character means the pattern must match from the very start of the string being tested and the $ sign says it must match right up to the very end. So here we are saying the string must only contain at least one or more digits form begining to end. If you want to allow an empty field you could swap the + for a * which means zero or more of whatever it follows.

    Your pattern could also be expressed like this:

    var reNum = /^[0-9]+$/ig; // only allow characters in the range 0 to 9 (ie digits)

    The square brackets allow you to define a list of allowable characters, and/or ranges of characters, any one of which can be contained in the string.

    If you want to allow for the user optionally entering a plus or minus sign in front of the number you could use:

    var reNum = /^[-+]?[0-9]+$/ig;

    The ? says make the preceeding character (in this case defined as a set) optional.

    If you make sure the user is only able to enter digits, you will save yourself the task of having to try and identify and remove unwanted stuff from the string and verify what you have left is legal.

    Hope someone will correct me if I have anything wrong here.

    Nick.


Bookmarks

Posting Permissions

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