473,396 Members | 1,599 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,396 software developers and data experts.

Validating

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
8 2000
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
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
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
"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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Alex Bink | last post by:
Hi, I have a validating event on a textbox in which I want to prevent the user to leave the textbox without entering the right data. Only if he clicks on another specific control he is allowed...
0
by: Joe | last post by:
Hi For a while now I have been finding postings of problems with the validating event not firing on controls properly. I too had this problem. The event would fire when clicking on another...
2
by: Chris Dunaway | last post by:
I have a form with a textbox and numerous panels, buttons and other controls. I have handled the textbox Validating and Validated events. The textbox will hold a filename. In the validating...
0
by: Matthew | last post by:
All, I have searched google and the newsgroups but can't find anything the same as what I am experiencing (though I may have missed something). I have controls (textboxes) within UserControls...
0
by: Gary Shell | last post by:
I am experiencing some strange behavior between a UserControl's validating event and a treeview control. Initially, I thought it was related to an issue in the Knowledgebase article 810852...
21
by: Darin | last post by:
I have a form w/ a textbox and Cancel button on it. I have a routine to handle textbox.validating, and I have the form setup so the Cancel button is the Cancel button. WHen the user clicks on...
4
by: easoftware | last post by:
I am using VS .Net 2003 and VB. I have an app with one parent and two Mdi child forms. I need to validate data in the Mdi form. The Form.Validating event works when I try to close a Mdi form,...
6
by: Ryan | last post by:
I have a windows form that I want to force validation on controls (text boxes) when the user clicks a "Save" button. The only way I've found to do this is to cycle through every control and call...
16
by: Al Santino | last post by:
Hi, It appears displaying a messagebox in a validating event will cancel the subsequent event. In the program below, button 2's click event doesn't fire if you open a dialog box in button 1's...
3
by: TheSteph | last post by:
Hi Experts ! I have a Winform Program in C# / .NET 2.0 I would like to ensure that a value in a TextBox is a valid Int32 when user get out of it (TextBox loose focus)
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
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,...

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.