473,503 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Identifying invalid char position using RegExp

Good morning.

I would like to use one or more RegExps to validate country names as having
the first and last words beginning with an uppercase letter, intermediate
words beginning with either uppercase or lowercase, and all other characters
being lowercase characters. For example:

Turks and Caicos

The difficult part is that I want to obtain save the string positions of the
invalid characters in an array, rather than just obtain a true or false
value.

Can anybody with more experience with Regular Expressions than me suggest
some code, sites, or approaches.

Thanks,
Ron
Jul 23 '05 #1
5 1548
Ron Brennan wrote on 20 nov 2004 in comp.lang.javascript:
Good morning.
Not for us overhee in Europe ;-)

I would like to use one or more RegExps to validate country names as
having the first and last words beginning with an uppercase letter,
intermediate words beginning with either uppercase or lowercase, and
all other characters being lowercase characters. For example:

Turks and Caicos

The difficult part is that I want to obtain save the string positions
of the invalid characters in an array, rather than just obtain a true
or false value.


simple testing is a Regex job:

s = 'Turks and Caicos'
alert(/^[A-Z][a-z]+ ([A-Za-z][a-z]+ )*[A-Z][a-z]+$/.test(s))

however your question is more a looping afair, not simple regex:

=======================

yourtest('Turks and Caicos')
yourtest('turKs anD cCaicos')
yourtest('turks And cCaicoS')

function yourtest(s){
stest = ' '
for (i=0;i<s.length;i++){
LastIsSpace = / /.test(stest)
stest = s.substr(i,1)
if (
i==0 &&
/[^A-Z]/.test(stest)
) // test first letter
document.write(i+' ff '+stest+'<br>')
else if (
LastIsSpace &&
/[^A-Z ]/.test(stest) &&
!/ /.test(s.substr(i,999))
) // test first letter last word
document.write(i+' ff '+stest+'<br>')
else if (
!LastIsSpace &&
/[^a-z ]/.test(stest)
) // test nonfirst letters
document.write(i+' FF '+stest+'<br>')
else // OK
document.write(i+' OK '+stest+'<br>')
}
document.write('<br><br>')
}

=======================

Please make your own array.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress,
but let us keep the discussions in the newsgroup)

Jul 23 '05 #2
Yes, that appears to be exacly what I wanted. I'll confirm that it works in
my application when I finish.

Thanks to Evertjan for the generous contribution of time and effort.

Ron

Jul 23 '05 #3
JRS: In article <RO********************@magma.ca>, dated Sat, 20 Nov
2004 10:16:27, seen in news:comp.lang.javascript, Ron Brennan
<rb******@magma.ca> posted :

I would like to use one or more RegExps to validate country names as having
the first and last words beginning with an uppercase letter, intermediate
words beginning with either uppercase or lowercase, and all other characters
being lowercase characters. For example:

Turks and Caicos


That policy fails for, among others,
British Virgin Islands
Campione d'Italia
Saint-Pierre et Miquelon
Turks and Caicos Islands
British Antarctic Territory
British Indian Territory
French Southern Territories
Juan de Nova Island
South Georgia and the South Sandwich Islands
Peñón de Vélez de la Gomera
and, of course,
the United States of America
Stato della Citta del Vaticano
the United Kingdom of Great Britain and Northern Ireland
Poblacht na h'Eireann
La Republique de la Cote d'Ivoire
Daulat Libiya al-'Arabiya al-Ishtrakiya al-Jumhuriya
Zhonghua Renmin Gongheguo
Chung-hua Min Kuo
and others.

Including Lord Howe Island; a place of interest that Win98 seems not to
be configurable for.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
Jul 23 '05 #4
That policy fails for, among others,
British Virgin Islands
Campione d'Italia
Saint-Pierre et Miquelon
Turks and Caicos Islands
British Antarctic Territory
British Indian Territory
French Southern Territories
Juan de Nova Island
South Georgia and the South Sandwich Islands
Peñón de Vélez de la Gomera
and, of course,
the United States of America
Stato della Citta del Vaticano
the United Kingdom of Great Britain and Northern Ireland
Poblacht na h'Eireann
La Republique de la Cote d'Ivoire
Daulat Libiya al-'Arabiya al-Ishtrakiya al-Jumhuriya
Zhonghua Renmin Gongheguo
Chung-hua Min Kuo
and others.

Including Lord Howe Island; a place of interest that Win98 seems not to
be configurable for.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. © Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm

etc.

The policy was a prototype I tentatively adoped when I didn't immediately
think of any of the many cases you cited. Obviously the policy has to be
rewritten and probably requires as part of it an enumeration of exceptions.
I'll see what I can come up with tomorrow and post it here for review.

Jul 23 '05 #5


Have you considered

1) doing this at the server
2) using 'dependent' listboxes at the client

Both would present the user with valid choices, rather than attempting
an algorithm to validate such a wide array of possibilities....

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #6

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

Similar topics

2
2354
by: Ron Brennan | last post by:
Good morning. I would like to use one or more RegExp to validate country names as having the first and last words beginning with an uppercase letter, intermediate words beginning with either...
1
5146
by: hokiegal99 | last post by:
This is not really a Python-centric question, however, I am using Python to solve this problem (as of now) so I thought it appropiate to pose the question here. I have some functions that search...
1
8222
by: Neil Morris | last post by:
Hi I have the following code that lists first names of people who's surnames are 'Morris'. What I am trying to do is search for first names that don't start at the beginning of the line ie. have...
3
2114
by: Christopher Weaver | last post by:
I want to set a value in a specific field in the current row of a DataSet. This seems like the most basic thing to do but I can't find the syntax for identifying the current row. IOW, I can do...
2
3032
by: Ste | last post by:
I generated a class from an XSD using XSDObjectGen, when i try to create an xml serialized string from it i get an invalid char in position 1,1 The code i use to serialise the object is pasted...
7
3422
by: Csaba Gabor | last post by:
I need to come up with a function function regExpPos (text, re, parenNum) { ... } that will return the position within text of RegExp.$parenNum if there is a match, and -1 otherwise. For...
1
7997
by: Java Guy | last post by:
I'm trying to view a web page. IE tells me there are (Java?) errors on the page. Here they are: Line: 15 Char: 7 Error: Wrong number of arguments or invalid propert assignment Code: 0 URL:...
10
4478
by: Frankie | last post by:
It appears that System.Random would provide an acceptable means through which to generate a unique value used to identify multiple/concurrent asynchronous tasks. The usage of the value under...
6
8366
by: =?ISO-8859-1?Q?Une_B=E9v?==?ISO-8859-1?Q?ue?= | last post by:
I wanted to test userAgent by a Regexp the goal : true if UA contains "AppleWebKit/528+ " or "AppleWebKit/525.12+ " note the final "+" false otherwise then i wrote :
0
7205
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
7093
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...
1
7006
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
5592
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
4685
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3166
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
397
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.