473,657 Members | 2,419 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(fo rmRef,fieldName ,min,max)var formField =
formRef.element s[fieldName];

if (!/^\d+(\.\d+)?$/.test(formField .value)) {
alert('Invalid entry...please try again!');
formField.focus ();
formField.selec t();
return false;
}
if ((formField.val ue < min) || (formField.valu e > max)) {
alert("The entry must be between " +min+ " and " +max+ ".");
formField.focus ();
formField.selec t();
return false;
}
Jul 20 '05 #1
5 1620
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.val ue < min) || (formField.valu e > 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.va lue < min) || (+formField.val ue > 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.va lue < min) || (+formField.val ue > 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.******@blueyo nder.co.uk.invalid (remove ".invalid" to reply)
Jul 20 '05 #4
On Mon, 24 Nov 2003 22:42:34 GMT
Michael Winter <M.******@bluey onder.co.uk.inv alid> 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.******@bluey onder.co.uk.
invalid> posted at Mon, 24 Nov 2003 22:42:34 :-

Alternativel y, 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.valu e
if (T < min || T > max ) {
}

IMHO, once one is accustomed to seeing such as

var T = +formField.valu e

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.demo n.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demo n.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
2796
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 script. I tried to do this onclick="document.form1.submit()" but than the button value is not posted and my php script is not working.
5
2597
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 validation on both. Is there a way to link the drop-down choice to the correspondent validation section while disabling validation for the other one? here's what I presently have:
2
1729
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 form). The OK button will continue to validate the form values, cancel to close window.
6
4840
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 selected and submitted. Thanks in advance for any help.
16
2223
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 individual fields, such as an "Email Validation Script" or a "Phone Validation Script". Is it ok to put all these scripts on page as they are or should they be joined in some way together to be one script? I'm a total JavaScript newbie and am completely...
5
1933
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 feedback. I have localized the problem to the definition of the form.onSubmit handler. Without my blocker, it looks like: onsubmit="ValidatorOnSubmit();" Since my javascript to block the double submit and change the image should only run if...
3
2403
by: Tarun Upadhyaya | last post by:
Hi, I am facing strange problem I read Scott mitchell's article about ASP.NET and javascript at http://www.msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/clientsidescript.asp where he has talked about creating a base class for displaying alerts and confirms after and before postbacks respectively. I used that Base class to inherit on of my code behind classes and and i
0
1189
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 class RangeValidator: BaseValidator,ICallbackEventHandler { public event ServerValidateEventHandler ServerValidate; String _controlToValidateValue;
3
2606
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 solve the problem. Here is the previous post http://bytes.com/forum/thread798289.html Trying to trace the problem I have written a code (Separate from The Form Validation) which sends 300 requests with an interval of 10 miliseconds and displays the...
2
3006
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 local system that validations work.plz tell that whats the problem in that. Here is my code <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html...
0
8395
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8310
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8826
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8732
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6166
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4155
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1955
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1615
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.