SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Oct 2002
    Location
    Madison, WI
    Posts
    198
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    easy regex question

    HI,

    How do I add blank space as acceptable in this expression?

    //checks for numbers, - or space only
    function checkPhone(frm,fld){
    obj = eval("window.document."+frm+"."+fld);
    tstval = obj.value;
    re = /^[0-9\-]+$/;
    if(!tstval.match(re)) {
    alert("Numbers or dashes only please.");
    obj.focus();
    return false; }
    else { return true; }
    }

    Thanks.
    :::::::::::::::::::::::::::::::::::::::::::::::::::
    Versa Studio
    ExpressionEngine experts
    bas (at) versa studio dot com

  2. #2
    SitePoint Wizard silver trophy rushiku's Avatar
    Join Date
    Dec 2003
    Location
    A van down by the river
    Posts
    2,056
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    /^[0-9\-]+$| {1}/;

  3. #3
    SitePoint Zealot
    Join Date
    Oct 2002
    Location
    Madison, WI
    Posts
    198
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rushiku
    /^[0-9\-]+$| {1}/;
    Thanks, but that doesn't appear to work.

    Let me clarify: I have a text field inside which I want to allow 0-9, -, and any number of blank spaces. So the field can be completely blank if desired.

    Please elaborate, thanks!
    :::::::::::::::::::::::::::::::::::::::::::::::::::
    Versa Studio
    ExpressionEngine experts
    bas (at) versa studio dot com

  4. #4
    SitePoint Wizard silver trophy rushiku's Avatar
    Join Date
    Dec 2003
    Location
    A van down by the river
    Posts
    2,056
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    from the doc, I assumed you were looking for numbers and dashes OR a space.

    Before i answer, i'm concerned about the original regex.
    I think you're validating phone numbers, but the regex as is will validate any combination ie: 123-456-7890 and 1-2-3-4-5-6-7-8-9 are both valid, for instance.

    If your format is 123-456-7890, you may prefer something more like

    ([0-9]{3}-){2}[0-9]{4}| +

    which matches only on 123-456-7890 format or any number of spaces

    anyway, if you want to add spaces to your regex change it to:

    [0-9\- ]+$|

    (insert a space after the dash)

    it allows any number/combination of numbers, dashes and spaces OR an empty
    string

    A good reference for regex building can be found here: http://java.sun.com/j2se/1.4.2/docs/...x/Pattern.html

  5. #5
    SitePoint Enthusiast kgish's Avatar
    Join Date
    Dec 2001
    Location
    Gouda, The Netherlands
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would not recommend restricting the type of string which can be entered, especially if you will later also be considering international numbers which can all be different in many unpredictable ways.

    In your case though, a better solution might be to provide the correct number of text input fields (3) each separated by a hard-coded hyphen, only allowing digits to be entered in each field, and the first two input fields automatically jumping (tabbing) to the next field after the third digit has been entered.

    With a little plain vanilla Javascript this can be accomplished without much difficulty.
    Kiffin
    Your average future-famous kind of guy...


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
  •