Connecting Tech Pros Worldwide Help | Site Map

Number Function

  #1  
Old July 20th, 2005, 12:07 PM
Mark
Guest
 
Posts: n/a
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


  #2  
Old July 20th, 2005, 12:07 PM
Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a

re: Number Function


Mark wrote:
[color=blue]
> 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.[/color]

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

  #3  
Old July 20th, 2005, 12:08 PM
Dr John Stockton
Guest
 
Posts: n/a

re: Number Function


JRS: In article <3F8C269C.80408@PointedEars.de>, seen in
news:comp.lang.javascript, Thomas 'PointedEars' Lahn
<PointedEars@web.de> posted at Tue, 14 Oct 2003 18:38:52 :-[color=blue]
> 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;
> }
> }[/color]


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.
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Prime Number Function SweetLeftFoot answers 4 December 5th, 2006 09:50 AM
format-number function & .NET Jim Craig answers 2 July 27th, 2006 08:55 AM
Round Number Function yulyos answers 0 November 22nd, 2005 04:02 PM
How can I get current line number, function name, etc. like in C++? Ken Varn answers 11 November 15th, 2005 07:03 AM
Round Number Function yulyos answers 0 July 21st, 2005 08:20 PM