473,480 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Script Value Form Validation

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
4 1796
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2495
by: Kenneth | last post by:
Hello, I'm having some serious problems debugging a script that I'm trying to make work. I'm working on a form where a user can type in a time (in the format of HH:MM), and another script...
2
2264
by: webbedfeet | last post by:
Hi I hope someone can help me. I have a client side form validation script which works perfectly in IE but clicking "Submit" in Mozilla does nothing - the form won't submit. Is there something I...
7
1387
by: Ray | last post by:
Hi all, I'm new to JavaScript and am trying to create a client side JavaScript form validation script. I'm doing ok with validating text input boxes but have a problem that I have not been able...
4
2442
by: Prodip Saha | last post by:
Dear ASP.NET Gurus, I have a TextBox control with AutoPostBack set to true to execute the server scripts. I also, added some client script for validation.What I want is--execute the client script...
6
1806
by: goober | last post by:
Ladies & Gentlemen: I have built a form that client-side validates and posts data to a CRM beautifully in Internet Explorer (IE) 6. The problem comes in FireFox (FF) 1.5 when everything works...
0
979
by: Suresh.Eddala | last post by:
Hi, I am trying to do Form validation on client side by using atlas client-centric script. Grouping all the field validation by using "validationGroup" and on button click event checking the...
4
3855
by: thanos | last post by:
Hello, I'm relatively new to PHP so I found this free contact us script on the net that i was going to use for my Contact Us php page. Its works pretty good except for error handling. I was...
1
47337
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Many websites have a form or a link you can use to download a file. You click a form button or click...
3
3877
by: happyse27 | last post by:
Hi All, I am creating the perl script using html form(with embedded javascript inside). When using this html form with javascript alone, it works where the form validation will pop up...
0
7037
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,...
1
6735
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
6895
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
5326
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4770
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...
0
2992
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
2977
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1296
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
176
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.