473,401 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

Number Function

Hi,

Im trying to figure out how I can have data validated in a text input form.
Basically the data entered into the form must consist of 2 uppercase letters
followed by 5 non negative numbers with no spaces in between. I want it to
display an error message if the letters arent in uppercase & if any of the
numbers are negative. Ive figured out how to validate text, but not a
mixture of text & numbers.

Any help on this would be greatly appriciated.

Regards

Mark
Jul 20 '05 #1
2 5577
Mark wrote:
Im trying to figure out how I can have data validated in a text input form.
Basically the data entered into the form must consist of 2 uppercase letters
followed by 5 non negative numbers with no spaces in between. I want it to
display an error message if the letters arent in uppercase & if any of the
numbers are negative. Ive figured out how to validate text, but not a
mixture of text & numbers.


Regular expressions are the method of choice here. You need to test if the
Regular expression matches against the value of a form element. If so, you
return true to the onsubmit event handler, false otherwise.

<script type="text/javascript" language="JavaScript">
<!--
function checkMe(s)
{
if (/^[A-Z]{2}[0-9]{5}$/.test(s))
return true;
else
{
alert("Error!"); // you should be more descriptive here ;-)
return false;
}
}
//-->
</script>

<form ... onsubmit="return checkMe(this.elements['foobar'].value)">
...
<input name="foobar" ...>
...
<input type="submit" ...>
</form>

See
http://devedge.netscape.com/library/...p.html#1010922
for details.

Keep in mind that server-side checking is also required as
client-side JavaScript can be disabled or not supported. The
client-side form checking can merely reduce network traffic
and server requests.
HTH

PointedEars

Jul 20 '05 #2
JRS: In article <3F************@PointedEars.de>, seen in
news:comp.lang.javascript, Thomas 'PointedEars' Lahn
<Po*********@web.de> posted at Tue, 14 Oct 2003 18:38:52 :-
function checkMe(s)
{
if (/^[A-Z]{2}[0-9]{5}$/.test(s))
return true;
else
{
alert("Error!"); // you should be more descriptive here ;-)
return false;
}
}

The following should be equivalent, and is IMHO easier to check :-

function checkMe(s) {
var OK = /^[A-Z]{2}\d{5}$/.test(s)
if (!OK) alert("Error!"); // you should be more descriptive here ;-)
return OK
}
--
© 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> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Gavin | last post by:
Hi, I'm a newbie to programming of any kind. I have posted this to other groups in a hope to get a response from anyone. Can any one tell me how to make my VB program read the Bios serial number...
7
by: netclectic | last post by:
Hi folks, i've searched and searched and can't find any example of what i'm trying to do. Essentially (i think) i need to add a new operator to Number. I'm using eval to evaluate expressions...
7
by: Shuffs | last post by:
Could someone, anyone please tell me what I need to amend, to get this function to take Sunday as the first day of the week? I amended the Weekday parts to vbSunday (in my code, not the code...
6
by: Penguin | last post by:
At some long ago time Steve Jorgensen answered thus: Subject: Re: How can I round a time? Newsgroups: comp.databases.ms-access Date: 1998/12/11 Access represents a date internally as a double...
11
by: Ken Varn | last post by:
I want to be able to determine my current line, file, and function in my C# application. I know that C++ has the __LINE__, __FUNCTION__, and __FILE___ macros for getting this, but I cannot find a...
27
by: Luke Wu | last post by:
Is there a C function that returns the number of digits in an input int/long? example: numdigits(123) returns 3 numdigits(1232132) returns 7
18
by: John Friedland | last post by:
My problem: I need to call (from C code) an arbitrary C library function, but I don't know until runtime what the function name is, how many parameters are required, and what the parameters are. I...
4
by: SweetLeftFoot | last post by:
Hello, i have designed some code that works out the first 250 prime numbers and prints them to the screen. However i need to implement 2 functions, one of which returns a 1 if the number is a prime...
4
by: moopower | last post by:
I have to do the following: For this project, you are to create a simple number guessing game. The program should begin by asking the user for a range (i.e. a minimum number and a maximum number)...
20
by: jacob navia | last post by:
Hi "How can I round a number to x decimal places" ? This question keeps appearing. I would propose the following solution #include <float.h> #include <math.h>
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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,...
0
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...

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.