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

Validating

P: n/a
Hi,

Is it possible to validate the 1st 2 characters in a text box as letter,
then the following 5 characters as numbers and if the input is incorrect for
it to display an alert?
It seems to be checking the entire field as opposed to the first 2
characters being letters & the following 5 being numbers.

The code I am using at the moment is:
function validateStudentNumber(studentNumber)
{
if (isBlank(studentNumber)) // blank?
{
alert("Enter your student number please in the format of 2 uppercase
letters followed by 5 numbers")
return false
}
for (var i = 0; i < studentNumber.length; i++) // letters?
{
var testChar = studentNumber.charAt(i)
if (testChar < "A" || testChar > "Z")
{
alert("Enter your student number please in the format of 2 uppercase
letters followed by 5 numbers")
return false
}
for (var i = 0; i < studentNumber.length; i++) // numeric?
{
var testChar = studentNumber.charAt(i)
if (testChar < "0" || testChar > "9")
}
alert("Enter your student number please in the format of 2 uppercase
letters followed by 5 numbers")
return false
}
Many Thanks

Mark


Jul 20 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Mark wrote:
Is it possible to validate the 1st 2 characters in a text box as letter,
then the following 5 characters as numbers and if the input is incorrect for
it to display an alert?^
Reads familiar to me, have you searched with Google Groups before posting?
It seems to be checking the entire field as opposed to the first 2
characters being letters & the following 5 being numbers.

The code I am using at the moment is:
[much too complicated :-)]


Use Regular Expressions (RegExp) instead:

var s = referenceToTextBox.value;
if (! /^\w{2}[0-9]{5}/.test(s))
alert("BOO!");
PointedEars

Jul 20 '05 #2

P: n/a
Thanks for that!

Am I implimenting this into my code right as I keep getting an Object
expected error:

function validateNumber(Number)
{
if (isBlank(Number)) // blank?
{
alert("Enter your number please in the format of 2 uppercase
letters followed by 5 numbers")
return false
}
var s = Number.value;
if (! /^\w{2}[0-9]{5}/.test(s))
{
alert("Enter your number please in the format of 2 uppercase
letters followed by 5 numbers");
return false
}
return true
}

Thanks for your help once again, sorry if the question seen dense but im new
to Javascripting

Mark

"Thomas 'PointedEars' Lahn" <Po*********@web.de> wrote in message
news:bo*************@ID-107532.news.uni-berlin.de...
Mark wrote:
Is it possible to validate the 1st 2 characters in a text box as letter,
then the following 5 characters as numbers and if the input is incorrect for it to display an alert?^


Reads familiar to me, have you searched with Google Groups before posting?
It seems to be checking the entire field as opposed to the first 2
characters being letters & the following 5 being numbers.

The code I am using at the moment is:
[much too complicated :-)]


Use Regular Expressions (RegExp) instead:

var s = referenceToTextBox.value;
if (! /^\w{2}[0-9]{5}/.test(s))
alert("BOO!");
PointedEars

Jul 20 '05 #3

P: n/a
Thomas 'PointedEars' Lahn <Po*********@web.de> writes:
Mark wrote:
Is it possible to validate the 1st 2 characters in a text box as letter,
then the following 5 characters as numbers and if the input is incorrect for
it to display an alert?^


Use Regular Expressions (RegExp) instead:

var s = referenceToTextBox.value;
if (! /^\w{2}[0-9]{5}/.test(s))


That actually fails the specification, since Word Characters (\w)
includes digits (which can be abbreviated as \d).

Unless the original poster wants international characters in the first
two positions, he can use
if (! (/^[a-z]{2}\d{5}/g).test(s))

He didn't say, but my guess is that there can be nothing after the
five digits, so the regular expression should probably have a "$"
before the last "/".

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #4

P: n/a
"Mark" <no*@chance.com> writes:
function validateNumber(Number)
The Number argument is a string containing the value of the input element.
Correct?
var s = Number.value;
That means that you shouldn't try to get the value again. Strings don't
have a property called "value". Just
var s = Number;
(or just use Number in the next line)
if (! /^\w{2}[0-9]{5}/.test(s))
You probably want
if (! (/^[A-Z]{2}\d{5}/).test(Number))

It only allows uppercase English letters.
This test also fails if the string is empty, so you don't need
the isBlank test.
{
alert("Enter your number please in the format of 2 uppercase
letters followed by 5 numbers");
Use "digits", not "numbers". Five numbers could be 42, 37, 1594323, pi
and -24 :)

"Thomas 'PointedEars' Lahn" <Po*********@web.de> wrote in message
news:bo*************@ID-107532.news.uni-berlin.de...


Please don't quote the entire previous message if you don't reply
directly to it. Trim your quotes, and preferably reply below quotes of
the parts you reply to (like I do :).

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #5

P: n/a
Thanks for your help guys, got it working a treat!!
"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote in message
news:k7**********@hotpop.com...
Thomas 'PointedEars' Lahn <Po*********@web.de> writes:
Mark wrote:
Is it possible to validate the 1st 2 characters in a text box as letter, then the following 5 characters as numbers and if the input is incorrect for it to display an alert?^
Use Regular Expressions (RegExp) instead:

var s = referenceToTextBox.value;
if (! /^\w{2}[0-9]{5}/.test(s))


That actually fails the specification, since Word Characters (\w)
includes digits (which can be abbreviated as \d).

Unless the original poster wants international characters in the first
two positions, he can use
if (! (/^[a-z]{2}\d{5}/g).test(s))

He didn't say, but my guess is that there can be nothing after the
five digits, so the regular expression should probably have a "$"
before the last "/".

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors:

<URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html> 'Faith without judgement merely degrades the spirit divine.'

Jul 20 '05 #6

P: n/a
Lasse Reichstein Nielsen wrote:
Thomas 'PointedEars' Lahn <Po*********@web.de> writes:
Mark wrote:
> Is it possible to validate the 1st 2 characters in a text box as letter,
> then the following 5 characters as numbers and if the input is incorrect for
> it to display an alert?^
Use Regular Expressions (RegExp) instead:

var s = referenceToTextBox.value;
if (! /^\w{2}[0-9]{5}/.test(s))


That actually fails the specification, since Word Characters (\w)
includes digits (which can be abbreviated as \d).


ACK, my bad.
Unless the original poster wants international characters in the first
two positions, he can use
if (! (/^[a-z]{2}\d{5}/g).test(s))


`/g' is inefficient here and the parantheses around the RegExp are not
required. But `/i' would be useful here because `letters' includes
lowercase *and* uppercase letters. Did you mix up both flags?
PointedEars

Jul 20 '05 #7

P: n/a
Thomas 'PointedEars' Lahn <Po*********@web.de> writes:
`/g' is inefficient here and the parantheses around the RegExp are not
required. But `/i' would be useful here because `letters' includes
lowercase *and* uppercase letters. Did you mix up both flags?


Yep. I meant "i". :)
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #8

P: n/a
JRS: In article <bo*************@ID-107532.news.uni-berlin.de>, seen in
news:comp.lang.javascript, Thomas 'PointedEars' Lahn
<Po*********@web.de> posted at Sun, 9 Nov 2003 19:37:35 :-
But `/i' would be useful here because `letters' includes
lowercase *and* uppercase letters.


Indeed it does.

But the error messages presented on the code in the first article in the
thread included two instances of

alert("Enter your student number please in the format of 2 uppercase
letters followed by 5 numbers")

which implies that the letters should be specified as A-Z, without i
modifier. The OP is apparently within the UK, so there is probably no
need to consider upper-case letters outside that range, such as .

--
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 #9

This discussion thread is closed

Replies have been disabled for this discussion.