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 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)
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.
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. :)
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) || ...) {
...
}
:)
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.
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! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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
|
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...
|
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 =...
|
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:
| |
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>
|
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...
|
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."; }
----------
|
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
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |