473,770 Members | 1,905 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need Help With Numeric Validation

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
6 2124
CSDunn wrote on 08 mei 2004 in comp.lang.javas cript:
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="retur n validate(this)" >
<input name="b1">
<input name="b2">
<input type=submit>
</form>
<script>
function validate(x){
err1=(/^\d{1,3}$/.test(x.b1.valu e))?"":"first box error"
err2=(/^\d{1,4}$/.test(x.b2.valu e))?"":"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
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="myHandl er.php" method="post" onsubmit="retur n validateForm()" >
<label for="FirstNumbe r">First Number: </label>
<input id="FirstNumber " name="FirstNumb er" type="text" size="5"
maxlength="3" />
<br />
<label for="SecondNumb er">Second Number: </label>
<input id="SecondNumbe r" name="SecondNum ber" type="text" size="5"
maxlength="4" />
<br />
<button type="submit">S ubmit 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["FirstNumbe r"].value;
if((onlyNums.te st(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
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["FirstNumbe r"].value;
var secondString = document.forms[0].elements["SecondNumb er"].value;

if((onlyNums.te st(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
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["FirstNumbe r"].value;
if((onlyNums.te st(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(fi rstString, 10).toString(10 )!=firstString) || ...) {
...
}

:)
Jul 23 '05 #5
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.demo n.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.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #6
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
8756
by: Jim | last post by:
Hi, I keep getting form results emailed to me that would indicate a form from my web site is getting submitted with all fields blank or empty, but my code should preventing users from proceeding if they left any field blank. My guess is that someone is trying to hack the site using the form to gain entry or run commands -- I don't really know since I'm not a hacker. I just know that forms are often susceptible to these kinds of...
4
2007
by: Adrienne | last post by:
I am the first to admit that I know bupkis about javascript, except that sometimes I need it to do something client side that I can't do server side. Anyway, here's my problem: <input type="text" name="ticket" id="ticket"> <input type="text" name="amount" id="amount"> My ASP script defaults ticket to be 30.00 and amount to be 600.00. What
11
2805
by: my-wings | last post by:
I think I've painted myself into a corner, and I'm hoping someone can help me out. I have a table of books (tblBooks), which includes a field (strPubName) for Publisher Name and another field (strPubCity) for Publisher City. These two fields have a many-to-one relationship with tables, (tlkpPubName and tlkpPubCity) respectively. The lookup tables only have one field (strPubName and strPubCity), which is their primary key. I also have...
2
2919
by: Chad | last post by:
I have a textbox control, txtMeasurement, that I want to allow only numeric decimal input. I thought to use a client side validation control to ensure that the data entered is of type "Double". 'Measurement Validation (Range Validator) Dim MeasurementRangeValidator As New RangeValidator 'validate that the measure is numeric MeasurementRangeValidator.ControlToValidate = txtMeasurement.ID| MeasurementRangeValidator.Display =...
2
9110
by: craig.wagner | last post by:
I have an element in my schema defined as follows: <xs:element name="BillingDate" type="xs:dateTime" nillable="true" minOccurs="0"/> I use the schema to validate incoming documents using an XmlValidatingReader in .NET 1.1. If the document contains the above element with no data, for example:
2
4894
by: this one | last post by:
I have the following code <html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <script language="JavaScript" src="..\include\gen_validatorv2.js" type="text/javascript"></script> </head>
2
1353
by: hassruby | last post by:
Can someone pls help me with some validation that im having a few technical problems with in my program. First of all, I will explain to you a little about what my program is suppose to do. It consists of a form with two text boxes that allow the user to enter numbers. These numbers will either be added, subtracted, divided or multiplied by each other. The user will type which operant that they wish to use by using an additional text boxed...
3
2086
by: aris1234 | last post by:
Hi.. i have field in record using type : numeric, how to make validation and i want viewing messagebox if user inputing character or empty? this code just work for empty field, not for numeric type, if(WithoutContent(document.add.sprice.value)) { errormessage += "\n\n\"PRICE\" still Empty."; } ----------
3
3672
by: Water Cooler v2 | last post by:
Sorry for asking this beginner question. I've written DTDs so far and read about XML Schemas. I understand that they are a replacement of the DTD fundamentally, and therefore allow for the validation of an XML document. My question really is: Why do we need XML Schemas other than for validation of an XML document? I am more interested in knowing if already available
0
9618
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
9454
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
10259
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
9906
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8933
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7456
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
5354
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
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2849
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.