Finding period/dot (.) in HTML form input using regular expressions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sudhakar Doddi

    Finding period/dot (.) in HTML form input using regular expressions

    Greetings,
    I am a novice in Javascript and I am not able to succeed finding right
    regular expression for my requirement.

    Input in HTML form should be in the format of row.rack.bin i.e
    three separate words separated by two dots/periods. User can enter
    "xyz.." also. Minimum two dots/periods must be present. Here is my
    code, it works partially. Please point me to the right expression.

    <script>

    locatorfmt=/[^\.]\.[^\.]\.[^\.]/i;

    function validateLocator () {
    locator=documen t.getElementByI d('p_item_locat ion').value;
    if (locator.search (locatorfmt)==-1) {
    alert('Please enter locator in correct format');
    return false;
    }
    return true;
    }

    </script>

    thanks
    Sudhakar
  • Chris Riesbeck

    #2
    Re: Finding period/dot (.) in HTML form input using regular expressions

    In article <bd4d8bf5.03082 20002.15847809@ posting.google. com>,
    i_am_sudhakar@h otmail.com (Sudhakar Doddi) wrote:
    [color=blue]
    >Input in HTML form should be in the format of row.rack.bin i.e
    >three separate words separated by two dots/periods. User can enter
    >"xyz.." also. Minimum two dots/periods must be present. Here is my
    >code, it works partially. Please point me to the right expression.[/color]

    There's a lot of ambiguity here. What characters
    can be in a "word?" letters, numbers, punctuation other
    than dot, spaces, tabs??? [^.] allows all those but
    do you want that?

    What does "minimum" mean? Is more OK?
    [color=blue]
    >locatorfmt=/[^\.]\.[^\.]\.[^\.]/i;[/color]

    No need for the "i" since there are no letters in the pattern.

    Dot doesn't need quoting inside [] -- [^.] is OK.

    You need [^.]* to allow zero or more non-dots.

    [^.] is "not dot" and [^.]* is zero or more not-dots, so

    locatorfmt = /[^.]*\.[^.]*\.[^.]*/

    [color=blue]
    >if (locator.search (locatorfmt)==-1) {[/color]

    search() looks everywhere. Do you really want to allow
    something like "Hi -- my name is Bob -- what's yours? ..."

    If you want locator to just have row.rack.bin, no
    extra dots, no spaces, use something like this:

    /^\S*\.\S*\.\S*/

    For lots of other pattern options, see


    Comment

    Working...