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

S

Sudhakar Doddi

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=document.getElementById('p_item_location').value;
if (locator.search(locatorfmt)==-1) {
alert('Please enter locator in correct format');
return false;
}
return true;
}

</script>

thanks
Sudhakar
 
C

Chris Riesbeck

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.

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?
locatorfmt=/[^\.]\.[^\.]\.[^\.]/i;

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 = /[^.]*\.[^.]*\.[^.]*/

if (locator.search(locatorfmt)==-1) {

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

http://www.visibone.com/regular-expressions/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,079
Messages
2,570,575
Members
47,207
Latest member
HelenaCani

Latest Threads

Top