Forum Moderators: open

Message Too Old, No Replies

JavaScript number format question

form validation string

         

Sangarius

8:44 pm on Mar 25, 2004 (gmt 0)

10+ Year Member



I am working on an application using JavaScript for the form validation. I have encountered a problem when trying to validate the entered numbers. I need the form only to accept a value in the format of
[1-9].[1-9][0-9] or just 0. Valid entry examples would be 5.2, 5.11 or 0. Invalid examples are anything not in those formats. As I am novice to JavaScript, I am not to sure on how to go about writing the validation string and I'm wondering if anyone knows how its done. The validation which I'm using now is as follows, but doesn't work for what I need:

if(!aform.txtheight.value.match(/^\d*\.\d{1,1}$/)) {
OutMessage = "Incorrect Measurement, please enter with a period i.e 5.7\n";
FocusControl = aform.txtheight;
}

Thank you for your help and time.

ajkimoto

9:39 pm on Mar 25, 2004 (gmt 0)

10+ Year Member



Sangarius,

First of all: Welcome to Webmaster World!

Here a regular expression that should do the trick:

/([1-9]+[\.]{1}[0-9]+¦[0]{1}[\.]?[0-9]*$)/

Either matches 1 to 9 followed by exactly one decimal point, followed by one or more 0-9 digits

OR

one 0 followed by zero or one decimal places, followed by zero or more 0-9 digits.

This accept responses like 2.4, 5.8, 0, 0.0, 0.124, etc. but will reject single digits like 2,4,5, etc.

Hope this helps,

ajkimoto

Sangarius

10:00 pm on Mar 25, 2004 (gmt 0)

10+ Year Member



Thank you Ajkimoto for your welcome and quick reply.

I have tried the string you suggested and I must be doing something wrong as now it accepts nothing, constantly displaying the alert message. I'm sure it's an easily fixed problem but as I am new at this, I again ask for your help. Here is the way I have the code now:

if(!aform.txtheight.value.match (/([1-9]+[\.]{1}[0-9]+¦[0]{1}[\.]?[0-9]*$)/ )) {
OutMessage = "Incorrect Measurement, please enter with a period i.e 5.7\n";
FocusControl = aform.txtheight;
}

Thank you,
Sangarius

ajkimoto

10:43 pm on Mar 25, 2004 (gmt 0)

10+ Year Member



Sangarius,

I rechecked the RE, and it appears to be correct. One thing that I did notice is that the 'pipe' character, ascii 124, seems to be replaced by ascii 166 by the BBS software, which will give you the behavior that you describe.

Try to replace the ¦ in the RE statement with the shift-backslash key and see if that works.

ajkimoto

Sangarius

11:00 pm on Mar 25, 2004 (gmt 0)

10+ Year Member



Ajkimoto,

It works perfectly, thank you so much!

Sangarius