By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,480 Members | 1,215 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,480 IT Pros & Developers. It's quick & easy.

Script Value Form Validation

P: n/a
Hello folks, I'm looking for a script to validate a specific number
value in a standard form input field.
An example would be, if someone enters a number into a form input, I
want the script to validate it and give an alert if that the number
exceeds the set script value.
Like if*the script value is set for 3000 and the number 3002 is
entered, I want an alert to pop and give a warning,

BTW - This script must be generic enough to work with older simple
browsers.

Hope someone can help.
PapaJo's friend (S_H)

Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Semi Head wrote:
Hello folks, I'm looking for a script to validate a specific number
value in a standard form input field.
An example would be, if someone enters a number into a form input, I
want the script to validate it and give an alert if that the number
exceeds the set script value.
Like if the script value is set for 3000 and the number 3002 is
entered, I want an alert to pop and give a warning,

BTW - This script must be generic enough to work with older simple
browsers.

Hope someone can help.
PapaJo's friend (S_H)


Hi

Your requerement is so basic, that the very earliest version of Javascript
could do it.
So don't worry.

This is how you proceed:
1) give your form a name (eg: myForm)
2) give the field you want to check a name (a textfield I suppose?) eg:
myTextfield

3) make an onChange-handler on that myTextfield that call some
validatingfunction.

So:

<form action="bla.php" name="myForm">
your age: <input type="text" onChange="checkValue();" name="myTextField">
<input type="submit">
</form>

<script type="text/javascript">
function checkValue(){
// get the value in
theValue = document.forms.myForm.myTextField.value;
if (theValue>3000) {
alert("Are you really that old?");
}
}
</script>
Regards,
Erwin Moller
Jul 23 '05 #2

P: n/a
Semi Head wrote:
Hello folks, I'm looking for a script to validate a specific number
value in a standard form input field.
An example would be, if someone enters a number into a form input, I
want the script to validate it and give an alert if that the number
exceeds the set script value.
Like if the script value is set for 3000 and the number 3002 is
entered, I want an alert to pop and give a warning,

BTW - This script must be generic enough to work with older simple
browsers.

Hope someone can help.


Gosh, this has been asked so many times....

function checkNum(n) {
var mx = 3000, // max value
mn = 0, // min value
msg = '';

if (n == parseInt(n,10)) {
msg = n + ' is an integer';
if (n >= mn && n <= mx) {
msg += '\nand it\'s within range';
} else {
msg += '\nbut it\'s outside the allowable range'
+ ' of ' + mn + ' to ' + mx;
}
} else {
msg = n + ' is not an integer';
}
return msg;
}
</script>

[...]

<label for="num">Enter a value (0 to 3000)
<input type="text" name="num" size="30" value="">
</label>
<input type="button" value="click me" onclick="
alert(checkNum(this.form.num.value));">

Untested on "older simple browsers" but I expect it will work on
anything that supports JavaScript and forms.

It can also be done with regEx, but I like the parseInt method as
it checks that it's only digits and converts it to an integer at
the same time.

If you want to allow scientific notation (e.g. 0.3e3), that's a
little harder but not impossible:

You have the choice of returning either the original entered text
or the parsed integer which will have leading zeros removed.

In regard to using "onchange", it is problematic as you can
change the field then click submit and in some browsers the field
doesn't lose focus so the onchange doesn't fire. It can also be
very frustrating for users if they can't leave a field until it
is properly validated, so generally validation is done onsubmit,
returning false to cancel the submit.

And lastly, have onscreen tips to let users know what the min and
max values are before they enter anything.

--
Fred
Jul 23 '05 #3

P: n/a
JRS: In article <28*****************@storefull-3118.bay.webtv.net>,
dated Fri, 7 Jan 2005 03:29:42, seen in news:comp.lang.javascript, Semi
Head <se*******@webtv.net> posted :
Hello folks, I'm looking for a script to validate a specific number
value in a standard form input field.
An example would be, if someone enters a number into a form input, I
want the script to validate it and give an alert if that the number
exceeds the set script value.
Like if*the script value is set for 3000 and the number 3002 is
entered, I want an alert to pop and give a warning,

BTW - This script must be generic enough to work with older simple
browsers.


I prefer to test such with a RegExp.

This works with IE4 :

function Chk(xx) { var x = xx.value, OK
OK = /^\d+$/.test(x)
if (!OK) { alert('Format!') ; xx.focus() ; return }
x = +x
if (x>3000) { alert('Value!') ; xx.focus() ; return }
return x }

Chk(F.X0)

The first + can be replaced by {1,4} if having too many digits is to be
considered a format error.

Or function Chk(xx) { var x = xx.value, OK
OK = /^\d+$/.test(x)
if (!OK || (x=+x)>3000) { alert('OW!') ; xx.focus() ; return }
return x }

Or function Chk(xx) { var x = xx.value, OK
OK = /^\d+$/.test(x)
if (OK && (x=+x)<=3000) return x
alert('OW!') ; xx.focus() ; return }

Or function Chk(xx) { var x = xx.value
if (/^\d+$/.test(x) && (x=+x)<=3000) return x
alert('OW!') ; xx.focus() ; return }
<URL:http://www.merlyn.demon.co.uk/js-valid.htm>.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #4

P: n/a
Originally Semi Head (S_H) wrote:
******Hello folks, I'm looking for a script to validate a
specific number value in a standard form input field.
****An example would be, if someone enters a number into a form
input, I want the script to validate it and give an alert if that the
number exceeds the set script value.
Like if the script value is set for 3000 and the number 3002 is entered,
I want an alert to pop and give a warning,
BTW - This script must be generic enough to work with older simple
browsers.
****************Hope someone can help.
******************************PapaJo's
friend (S_H)
-------------------------------------------

Response #1

From: si**********************************...y ourself.com
(Erwin*Moller)

Hi
Your requerement is so basic, that the very earliest version of
Javascript could do it.
So don't worry.
This is how you proceed:
1) give your form a name (eg: myForm)
2) give the field you want to check a name (a textfield I suppose?) eg:
myTextfield
3) make an onChange-handler on that myTextfield that call some
validatingfunction.

So:
<form action="bla.php" name="myForm">
your age: <input type="text" onChange="checkValue();"
name="myTextField">
<input type="submit">
</form>
<script type="text/javascript">
*function checkValue(){
******// get the value in
******theValue = document.forms.myForm.myTextField.value;
* if (theValue>3000) {
**********alert("Are you really that old?");
****}
*}
</script>

Regards,
Erwin Moller

-------------------------------------------

Response #2

From: oz****@iinet.net.auau (Fred*Oz)

****Gosh, this has been asked so many times....
****function checkNum(n) {
**var mx = 3000, * // max value
************mn = 0, * * // min value
******msg = '';
****if (n == parseInt(n,10)) {
****msg = n + ' is an integer';
****if (n >= mn && n <= mx) {
************msg += '\nand it\'s within range';
****} else {
************msg += '\nbut it\'s outside the
allowable range'
**********************+ ' of
' + mn + ' to ' + mx;
********}
****} else {
****msg = n + ' is not an integer';
****}
****return msg;
****}
</script>
[...]
******<label for="num">Enter a value (0 to 3000) * *
<input type="text" name="num" size="30" value="">
******</label>
******<input type="button" value="click me" onclick=" *
* alert(checkNum(this.form.num.value));">
****Untested on "older simple browsers" but I expect it will
work on anything that supports JavaScript and forms.
****It can also be done with regEx, but I like the parseInt
method as it checks that it's only digits and converts it to an integer
at the same time.
****If you want to allow scientific notation (e.g. 0.3e3),
that's a little harder but not impossible:
****You have the choice of returning either the original entered
text or the parsed integer which will have leading zeros removed.
****In regard to using "onchange", it is problematic as you can
change the field then click submit and in some browsers the field
doesn't lose focus so the onchange doesn't fire. It can also be very
frustrating for users if they can't leave a field until it is properly
validated, so generally validation is done onsubmit, returning false to
cancel the submit.
****And lastly, have onscreen tips to let users know what the
min and max values are before they enter anything.
--
Fred
------------------------------------------

Response #3

From: sp**@merlyn.demon.co.uk (Dr*John*Stockton)

I prefer to test such with a RegExp.
This works with IE4 :
****************function Chk(xx) { var x
= xx.value, OK
**********OK = /^\d+$/.test(x)
********************if (!OK) {
alert('Format!') ; xx.focus() ; return }
********************x = +x
********************if (x>3000)
{ alert('Value!') ; xx.focus() ; return }
**********return x }
****************Chk(F.X0)
The first + can be replaced by {1,4} if having too many digits is to be
considered a format error.
Or * * function Chk(xx) { var x = xx.value, OK
**********OK = /^\d+$/.test(x)
********************if (!OK ||
(x=+x)>3000) { alert('OW!') ; xx.focus() ; return }
**********return x }
Or * * function Chk(xx) { var x = xx.value, OK
**********OK = /^\d+$/.test(x)
********************if (OK &&
(x=+x)<=3000) return x
**********alert('OW!') ; xx.focus() ; return }
Or * * function Chk(xx) { var x = xx.value
**********if (/^\d+$/.test(x) && (x=+x)<=3000)
return x
**********alert('OW!') ; xx.focus() ; return }
---------------------------------------------------
Sorry, i was unable to smallify the text of the posted responses do to
browser limitations.
i hope my response will be accepted as adequate.
Thank you all, for posting your scripts & suggestions.
All of them worked but i believe Erwin Moller's script will be the best
for Papajoe's WebTV browser application.
Thank you again!
You are a fair & generous group of JS scripting professionals

take care,
Papajoe's friend (S_H)

Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.