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

Need Help With Numeric Validation

P: n/a
Hello,
I need to apply validation to two text boxes. One text box named
FirstNumber is required to contain only numbers, and be only three
digits long.

The other text box named SecondNumber needs to also require only
numbers, but allow for four digits.

I need to have a msg box appear after the user clicks a button named
'submit' if the validation on either text box has been violated.

I spend most of my time writing SQL, and I'm not well versed at all
with JavaScript.

Please help!

Thanks!

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


P: n/a
CSDunn wrote on 08 mei 2004 in comp.lang.javascript:
I need to apply validation to two text boxes. One text box named
FirstNumber is required to contain only numbers, and be only three
digits long.

The other text box named SecondNumber needs to also require only
numbers, but allow for four digits.

I need to have a msg box appear after the user clicks a button named
'submit' if the validation on either text box has been violated.


<form onsubmit="return validate(this)">
<input name="b1">
<input name="b2">
<input type=submit>
</form>
<script>
function validate(x){
err1=(/^\d{1,3}$/.test(x.b1.value))?"":"first box error"
err2=(/^\d{1,4}$/.test(x.b2.value))?"":"second box error"
if(err1+err2!=""){
alert(err1+"\n"+err2)
return false
}
return true
}
</script>
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 23 '05 #2

P: n/a
Ron
CSDunn wrote:
Hello,
I need to apply validation to two text boxes. One text box named
FirstNumber is required to contain only numbers, and be only three
digits long.

The other text box named SecondNumber needs to also require only
numbers, but allow for four digits.

I need to have a msg box appear after the user clicks a button named
'submit' if the validation on either text box has been violated.

I spend most of my time writing SQL, and I'm not well versed at all
with JavaScript.

Please help!

Thanks!

CSDunn

The textbox maxlength can be taken care of through HTML:
<form action="myHandler.php" method="post" onsubmit="return validateForm()">
<label for="FirstNumber">First Number: </label>
<input id="FirstNumber" name="FirstNumber" type="text" size="5"
maxlength="3" />
<br />
<label for="SecondNumber">Second Number: </label>
<input id="SecondNumber" name="SecondNumber" type="text" size="5"
maxlength="4" />
<br />
<button type="submit">Submit Form</button>
</form>
Somewhere in your head:
<script type="text/javascript">
function validateForm() {
var onlyNums = new RegExp(/^\D+$/); // matches one or more non-digits.
var firstString = document.forms[0].elements["FirstNumber"].value;
if((onlyNums.test(firstString)) || (onlyNums.test(secondString))) {
alert("You can only use numbers in this form.");
return false;
}
else {
return true;
}
}
</script>
Javascript uses Perl-type regular expressions.
Jul 23 '05 #3

P: n/a
Ron
Ron wrote:
<script type="text/javascript">
function validateForm() {
var onlyNums = new RegExp(/^\D+$/); // matches one or more non-digits.
var firstString = document.forms[0].elements["FirstNumber"].value;
var secondString = document.forms[0].elements["SecondNumber"].value;

if((onlyNums.test(firstString)) || (onlyNums.test(secondString))) {
alert("You can only use numbers in this form.");
return false;
}
else {
return true;
}
}
</script>


I realized I didn't declare secondString as a variable, but you get the
idea. :)
Jul 23 '05 #4

P: n/a
Ron
Ron wrote:
CSDunn wrote:
Hello,
I need to apply validation to two text boxes. One text box named
FirstNumber is required to contain only numbers, and be only three
digits long.
[snip]


[snip]
<script type="text/javascript">
function validateForm() {
var onlyNums = new RegExp(/^\D+$/); // matches one or more non-digits.
var firstString = document.forms[0].elements["FirstNumber"].value;
if((onlyNums.test(firstString)) || (onlyNums.test(secondString))) {
alert("You can only use numbers in this form.");
return false;
}
else {
return true;
}
}
</script>
Javascript uses Perl-type regular expressions.


Hmm, due to the nature of the problem, instead of the regular expression
test, you could also use:

if((parseInt(firstString, 10).toString(10)!=firstString) || ...) {
...
}

:)
Jul 23 '05 #5

P: n/a
JRS: In article <80**************************@posting.google.com >, seen
in news:comp.lang.javascript, CSDunn <cd***@valverde.edu> posted at Fri,
7 May 2004 15:17:35 :
I need to apply validation to two text boxes. One text box named
FirstNumber is required to contain only numbers, and be only three
digits long.
You mean only decimal digits. Untested, but probably right :-

S = ...FirstNumber.value
OK = /^\d\d\d$/.test(S)
if (!OK) { ... }
The other text box named SecondNumber needs to also require only
numbers, but allow for four digits.


S = ...SecondNumber.value
OK = /^\d{4}$/.test(S)
if (!OK) { ... }
<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://jibbering.com/faq/> Jim Ley's FAQ for 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 #6

P: n/a
My thanks to everyone for your help!

CSDunn
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.