473,503 Members | 2,136 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem with validation script

Hi, I need to validate a hi/low range, allowing decimal numbers. For
this example, the low is 1, high is 5, but something is not working. I
got this script from someone helping me in this group, but I must have
done something wrong. When I enter 7, it prompts me as an incorrect
entry, as expected. However, when I enter 22 or 23, etc. the script
passes those entries (perhaps accepting individual numbers between 1
and 5. Sorry, but the regular expression stuff is hardly my strong
suit...

Please tell me where I've gone astray! Thanks, Kathy

function ValidateSave(formRef,fieldName,min,max)var formField =
formRef.elements[fieldName];

if (!/^\d+(\.\d+)?$/.test(formField.value)) {
alert('Invalid entry...please try again!');
formField.focus();
formField.select();
return false;
}
if ((formField.value < min) || (formField.value > max)) {
alert("The entry must be between " +min+ " and " +max+ ".");
formField.focus();
formField.select();
return false;
}
Jul 20 '05 #1
5 1613
Lee
KathyB said:

Hi, I need to validate a hi/low range, allowing decimal numbers. For
this example, the low is 1, high is 5, but something is not working. I
got this script from someone helping me in this group, but I must have
done something wrong. When I enter 7, it prompts me as an incorrect
entry, as expected. However, when I enter 22 or 23, etc. the script
passes those entries (perhaps accepting individual numbers between 1
and 5. Sorry, but the regular expression stuff is hardly my strong
suit...

Please tell me where I've gone astray! Thanks, Kathy
if ((formField.value < min) || (formField.value > max)) {


The problem is not with the regular expressions, but with the
comparisons. Field values are strings, and so the comparisons
are being done as string comparisons, and the string "22" is
less than the string "5".

You need to convert the values to integers before comparing.
One simple way to do that is to apply the unary plus sign to
them. Since that operator only applies to numbers, the engine
will automatically convert the values to numbers for you:

if ((+formField.value < min) || (+formField.value > max))

Jul 20 '05 #2
THANK YOU, Lee!!!

Kathy

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #3
Lee wrote on 24 Nov 2003:

<snip>
You need to convert the values to integers before comparing.
One simple way to do that is to apply the unary plus sign to
them. Since that operator only applies to numbers, the engine
will automatically convert the values to numbers for you:

if ((+formField.value < min) || (+formField.value > max))


Alternatively, you could use the constructor of the Number object:

if (( Number( formField.value ) < min )
|| ( Number( formField.value ) > max )) {
}

I personally prefer the second, though they both result in the same
behaviour. Using Number is a little more readable, and doesn't run
the risk of doing something unintended, like concatenating (if you
placed another argument in front of the plus in Lee's example).

Mike

--
Michael Winter
M.******@blueyonder.co.uk.invalid (remove ".invalid" to reply)
Jul 20 '05 #4
On Mon, 24 Nov 2003 22:42:34 GMT
Michael Winter <M.******@blueyonder.co.uk.invalid> wrote:
<snip>
Alternatively, you could use the constructor of the Number object:

if (( Number( formField.value ) < min )
|| ( Number( formField.value ) > max )) {
}

I personally prefer the second, though they both result in the same
behaviour. Using Number is a little more readable, and doesn't run
the risk of doing something unintended, like concatenating (if you
placed another argument in front of the plus in Lee's example).


I agree. I always prefer the obvious to non-intuitive idioms.

--
Then there was the man who drowned crossing a stream with an average
depth of six inches.
-- W. I. E. Gates
Jul 20 '05 #5
JRS: In article <Xn*******************************@193.38.113.46 >, seen
in news:comp.lang.javascript, Michael Winter <M.******@blueyonder.co.uk.
invalid> posted at Mon, 24 Nov 2003 22:42:34 :-

Alternatively, you could use the constructor of the Number object:

if (( Number( formField.value ) < min )
|| ( Number( formField.value ) > max )) {
}


Rather than that, I suggest

var T = Number( formField.value ) // or +formField.value
if (T < min || T > max ) {
}

IMHO, once one is accustomed to seeing such as

var T = +formField.value

one will on reading it readily see that it is a means of getting what is
wanted (a number) from what is available (a field, holding a string),
and will not be over-tempted to fiddle with it inappropriately.

AFAICS, a + directly after an assignment operator must be a unary
arithmetic operator, whose only purpose can be conversion to number.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
2787
by: Bartosz Wegrzyn | last post by:
Please look at my code. I do validation of my form before I submit it. I want to be able to to press one form button without running validating script. I just want to go directly to my php...
5
2568
by: EviL KerneL | last post by:
Hi - I am trying to figure out a way to enforce the validation included for this form based on whether the user chooses "email" or "phone" as the contact choice. Right now it is set to enforce...
2
1719
by: kelvin | last post by:
Hi, I'm having this problem and I can't seem to solve it. I've created a confirmation page. The page displays the form field data and has 2 links - OK (to continue) and Cancel (go back to...
6
4829
by: MickG | last post by:
Hi, I am trying to validate these values, this seems to work fine for the phone number and name but I am trying to get the program to fail to submit and set the focus on the date when 2006 is...
16
2192
by: Hosh | last post by:
I have a form on a webpage and want to use JavaScript validation for the form fields. I have searched the web for form validation scripts and have come up with scripts that only validate...
5
1915
by: Jim Brandley | last post by:
I have a required field with a client-side validator. It works as advertised until I try to add code to block double submits and replace the image on the submit button to give the users some...
3
2397
by: Tarun Upadhyaya | last post by:
Hi, I am facing strange problem I read Scott mitchell's article about ASP.NET and javascript at ...
0
1183
by: Dhanashree | last post by:
I have a RangeValidator which uses AJAX to call server-side validation function from the client. The code works fine, but I have one problem. following is my range validator class: public...
3
2595
rizwan6feb
by: rizwan6feb | last post by:
Hi experts! Recently i was working on "Form Validation Using Ajax". My form validation was creating problem, when a user changes focus too quickly. I had a post related to this, but was unable to...
2
2996
by: swethak | last post by:
hi , i write the code in .htm file. It is in cgi-bin/searches/one.htm.In that i write a form submitting and validations.But validations are not worked in that .htm file. I used the same code in my...
0
7204
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7091
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7282
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7342
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6998
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7464
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
3171
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1516
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
391
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.