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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regular Expressions

Status
Not open for further replies.

KMarshall

Technical User
May 25, 2001
12
GB
I've been trying to create a regular expression to validate a string, but since this is my first time with regular expressions, I'm making very hard work of it.

The accepted string format is NNNNAA where N=numberic and A=Alpha. The field is forced uppercase.

I can validate the NNNN part but can't work out how to also check the AA part.

Here's what I have so far:
<Sctipt Language="JavaScript">
function check(){
var rExp=/^\d{4}$/ //Allowed NNNNAA

if (document.myForm.inputField.value.search(rExp)==-1){ //if match failed
alert('Invalid')
}else{
alert('Ok')
}
}
</Script>

<form name="myForm" method="post">
<input type="text" name="inputField" value="" onBlur="check()" size="6" maxlength="6">
</form>



Can anyone offer any assistance.

Thanks.
Kevin.
 
var rExp=/^\d{4}[a-z][a-z]$/i <-- the "i" at the end makes the expression insensitive... a or A if you only want to accept uppercase then change the [a-z] to [A-Z] and get rid of the "i"

grtfercho çB^]\..
"Imagination is more important than Knowledge" A. Einstein
-----------------------------------------------
 
optionally
var rExp=/^\d{4}[a-z]{2}$/

grtfercho çB^]\..
"Imagination is more important than Knowledge" A. Einstein
-----------------------------------------------
 
FWIW
dunno if it helps but IE5.5, NS 4.5 and Firefox all do things differently and what I found was that often somewhat like this helped

// get it into a variable first
var fred = document.myForm.inputField.value;
alert(fred)
// then use it
var bill =fred.search(rExp)
alert(bill)
if (bill==-1){etc;}

I found that testing for null before a search or substring was essential or it just stops and does nothing and doesn't tell you (sound familiar?)

ie
if (fred){ // true if not null
if (fred.search(rExp)==-1){
alert("so that's it!")
}
}

good luck.

OH never used it in real situations but there is the method

try{
fred = iffycode(fred)
}
catch
{
alert("this is wrong - fred = iffycode(fred)")
}
 
Thanks for the very fast reply guys!

I've gone with grtfercho's answer as it is the easiest one for me to understand although I do appreciate what Cresby is saying about the nulls.

I have one more thing to add to the mix. What if I need an optional character on the end? The last character can either be nothing or a 'Z'.

Is this possible?

The format is AAAANNZ
 
Actually
var rExp=/^\d{4}[a-z]{2,3}$/

will catch 1234ABC and 1234AZ
from what I understand the last character can be present and if it is, it has to be a Z.
therefore this will give you better results

var rExp=/^\d{4}[a-z]{2}Z?$/


grtfercho çB^]\..
"Imagination is more important than Knowledge" A. Einstein
-----------------------------------------------
 
Brilliant, thanks grtfercho.
I was struggling with that very problem after my initial excitement. Thanks!
 
hey guys, have been reading this string...although old, maybe i'll get a response.

i'm using the following code:
Code:
function alphaCheck(x) {
alphaOnly='no';
 if (x!='') {
   for (c=0; c<x.length; c++) {
    if (x.substr(c,1).match(/[a-zA-Z]/) != null) {
    alphaOnly='yes';
    }
   }
 }
return alphaOnly;
}

it will catch the number if there is only numbers in the field...but as long as there is a alpha character in there somewhere, it doesn't throw a red flag.

my goal is to search and confirm the value is all alpha only, no symbols, no numbers, etc.

any ideas is appreciated.

- g
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top