473,397 Members | 2,028 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,397 software developers and data experts.

Validating a string.

Hello,

I have a requirement whereby I must validate that a string matches one of
several patterns:

1. An ISO standard 2 character country code (eg GB, NL) followed by five
digits.
2. An ISO standard 2 character country code (eg GB, NL) followed by an X
followed by five digits.
3. 3 characters in the range a-z followed by 3 digits followed by 2
characters in a specific set (eg MT, AT, DA).
4. 4 characters followed by a hash (#) followed by 3 characters followed by
3 digits followed by a hash followed by 3 characters.
5. 4 characters followed by a hash (#) followed by 3 characters followed by
3 digits followed by a hash followed by 4 characters.

I have been told that a better way to achieve this than using .charAt is by
using something called a regular expression, but I don't really understand
them. Can anyone please advise on the above or alternatively point me in
the direction of a suitable resource on these regular expression things?

Cheers,
Adam M.
Jul 20 '05 #1
2 2642
This one is quite good:

http://www.javascriptkit.com/javatutors/re.shtml
"Adam David Moss" <ad****@nospamglobalnet.co.uk> wrote in message
news:bf**********@sparta.btinternet.com...
Hello,

I have a requirement whereby I must validate that a string matches one of
several patterns:

1. An ISO standard 2 character country code (eg GB, NL) followed by five
digits.
2. An ISO standard 2 character country code (eg GB, NL) followed by an X
followed by five digits.
3. 3 characters in the range a-z followed by 3 digits followed by 2
characters in a specific set (eg MT, AT, DA).
4. 4 characters followed by a hash (#) followed by 3 characters followed by 3 digits followed by a hash followed by 3 characters.
5. 4 characters followed by a hash (#) followed by 3 characters followed by 3 digits followed by a hash followed by 4 characters.

I have been told that a better way to achieve this than using .charAt is by using something called a regular expression, but I don't really understand
them. Can anyone please advise on the above or alternatively point me in
the direction of a suitable resource on these regular expression things?

Cheers,
Adam M.

Jul 20 '05 #2
JRS: In article <bf**********@sparta.btinternet.com>, seen in
news:comp.lang.javascript, Adam David Moss
<ad****@nospamglobalnet.co.uk> posted at Sun, 20 Jul 2003 16:35:38 :-
Hello,

I have a requirement whereby I must validate that a string matches one of
several patterns:

1. An ISO standard 2 character country code (eg GB, NL) followed by five
digits.
2. An ISO standard 2 character country code (eg GB, NL) followed by an X
followed by five digits.
3. 3 characters in the range a-z followed by 3 digits followed by 2
characters in a specific set (eg MT, AT, DA).
4. 4 characters followed by a hash (#) followed by 3 characters followed by
3 digits followed by a hash followed by 3 characters.
5. 4 characters followed by a hash (#) followed by 3 characters followed by
3 digits followed by a hash followed by 4 characters.

I have been told that a better way to achieve this than using .charAt is by
using something called a regular expression,
Agreed.
but I don't really understand
them. Can anyone please advise on the above
Some may construct a single vast RegExp to do it. I would merge 12 & 45
only. Ignoring that some 2-letter patterns are not at present
countries, to get started,

function TryIt(S) {
if (/^([A-Z]{2})X?\d{5}$/.test(S)) return 12
if (/^([a-z]{3})\d{3}([A-Z]{2})$/.test(S)) return 3
if (/^....#...\d{3}#....?$/.test(S)) return 45
return 0 } // lightly tested

The result may be used as, or cast to, a boolean; or used to show which
rule fired.

In the first two cases, RegExp.$1 (& RegExp.$2) contained matched
fields, which can be tested against a list of valid ones :

OK = "UK NL RU MX ...".indexOf(RegExp.$1+" ") != -1 // lightly tested
or alternatively point me in
the direction of a suitable resource on these regular expression things?


Dillons, Waterstones, maybe Foyles, etc.; any good Public, College, or
University Library.

Before going, read <URL:http://www.merlyn.demon.co.uk/js-other.htm#RE>,
to get a slight general idea.

--
© 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

1
by: Brian Kedersha | last post by:
I found code for validating XML documents with the XML Schema cashed for rapid access. Example 3: Validating with XMLSchemaCache. ...
1
by: Christian | last post by:
Hi, I load an Xml-file "customers.xml" into a DataSet (works fine) but then how do I validate it against a schema (e.g. customers.xsd) ? my customers.xml: <?xml version="1.0"...
6
by: Robert Reineri | last post by:
Hello, New to the XML world and .NET. I have what I believe to be a simple problem, but I have read the .NET docs till I'm blue in the face and still can't locate a simple example of how to...
1
by: Andy | last post by:
I am having some trouble validating XML using the XmlValidatingReader. I have created some xml and used the visual studio to generate the schema. So I am confident that the xml and schema match. ...
2
by: Joris Janssens | last post by:
I'm trying to write a program for validating XHTML 1.1-documents against the XHTML 1.1 DTD (which is actually the same as validating an XML-file) but I always get a "(404) Not found" error. This...
1
by: Craig Beuker | last post by:
Hello, I am experimenting with this XmlValidatingReader and have a question about how it is working (or not working as would be the case) The sample documents and code are included at the end...
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...
5
by: ameen.abdullah | last post by:
Hi Guys, I have a textbox in windows form that should only accept alphabets, numbers, spaces and underscore. If the textbox contains anyother character it should display a msg at the time of...
1
by: =?Utf-8?B?bGpsZXZlbmQy?= | last post by:
I've noticed that controls do not raise a Validating event if they are contained in a ToolStripDropDown via a ToolStripControlHost item. Please run the following sample and follow the instructions...
6
by: Richard | last post by:
I'm validating a date and time string which must be EXACTLY of the format yy-mm-dd hh:mm:ss and extracting the six numeric values using sscanf. I'm using the format string "%2u-%2u-%2u...
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
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
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
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...

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.