Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regular expressions for data checking 1

Status
Not open for further replies.

wbodger

Programmer
Apr 23, 2007
769
US
I have two fields that I have to check for a certain format, which is:

First character Alpha, the rest alpha-numeric or underscore and for one of the two fields I have to enforce all uppercase. I am new to RegExp and had thought that \^[A-Z]\w$ would give me what I need, but that does not seem to be the case. Can anybody out there lend a hand and perhaps explain to me what I am missing?

Thanks,
WB
 
This
Code:
\b[A-Z]+[A-Z0-9_]{0,}\b
seems to mostly work though it allows for spaces which I need to not allow.
 
When I tried using the caret it did not work for me and when I test this it does not work either. What am I missing? I am just trying to verify against one of the online testers.

wb
 
unless you use the same engine, you may not get the same results. you should make sure that you are using a javascript engine.

your requirements are these

Code:
Regex 1

1. first character must be a case insensitive alphabetic character
2. all other characters must be either a case insensitive alphabetic character or a digit or an underscore
Regex1   ^[A-z][A-z0-9_]+$ 
^  start of string
[A-z] first character must be any character from capital A to little z
[A-z0-9_] second character must be any character from capital A to little z or an underscore or a digit from 0-9
+ previous character must exist at least once
$ end of string
[/i] the / denotes the start and end of the regex.  the i makes the whole thing case insensitive.  belt and braces in this case

Code:
Regex 2

1. first character must be an uppercase alphabetic character
2. all other characters must be either an uppercase alphabetic character or a digit or an underscore
Regex2   ^[A-Z][A-Z0-9_]+$ 
^  start of string
[A-Z] first character must be any character from capital A to capital Z
[A-Z0-9_] second character must be any character from capital A to capital z or an underscore or a digit from 0-9
+ previous character must exist at least once
$ end of string

to my mind, the two regular expressions I posted match your stated requirements. I have also created a jsFiddle to evidence this. See here for the jsFiddle. (if it hangs on 'creating the awesome', just press the run button on the top menu. That will sort things out.

So if this is not working for you, you are either doing something wrong in your implementation code or you have not correctly articulated your requirements.
 
Looks great, not sure what I was doing wrong. That is the functionality and pretty close to what I thought, but I was definitely missing something. Thanks~!

wb
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top