Regular expression for required alpha and numeric characters

N

.Net Sports

I am checking for text input on a form validation in javascript that
required at least one numeric character along with any number of alpha
characters for a given input text box. The below is a var declare that
does a method to check for alpha, or numeric, or - _ characters

var charpos = objValue.value.search("[^A-Za-z0-9\-_]");

but doing:

var charpos = objValue.value.search("[^A-Za-z0-9]");

....doesnt work.

????
netsports
 
S

shimmyshack

I am checking for text input on a form validation in javascript that
required at least one numeric character along with any number of alpha
characters for a given input text box. The below is a var declare that
does a method to check for alpha, or numeric, or - _ characters

var charpos = objValue.value.search("[^A-Za-z0-9\-_]");

but doing:

var charpos = objValue.value.search("[^A-Za-z0-9]");

...doesnt work.

????
netsports

var value = 'hi there';
var RegExp = /^[A-Za-z0-9\-_]{6,16}$/;

if( RegExp.test(value) )
{
alert( 'yipee' );
}

of course best to limit the max number of chars as well as the min,
here it's 6 to 16.
 
N

.Net Sports

Thanks for reply, but what I need is to have users enter a proposed
password, whereas the password has to have at least one numeric
character, and yes your idea of between 6 and 16 character range is
acceptable, but what I have is a switch-case routine, whereas one of
the case statements as shown below checks for just alpha character :

case "alpha":
{
var charpos = objValue.value.search("[^A-Za-z]");
if(objValue.value.length > 0 && charpos >= 0)
{
if(!strError || strError.length ==0)
{
strError = objValue.name+": Only alphabetic
characters allowed ";
}//if
alert(strError + "\n [Error character position " +
eval(charpos+1)+"]");
return false;
}//if
break;
}

....and i'm having trouble figuring how to add the added requirement of
"at least one numeric character" constraint.
thanx
Netsports

I am checking for text input on a form validation in javascript that
required at least one numeric character along with any number of alpha
characters for a given input text box. The below is a var declare that
does a method to check for alpha, or numeric, or - _ characters
var charpos = objValue.value.search("[^A-Za-z0-9\-_]");
but doing:
var charpos = objValue.value.search("[^A-Za-z0-9]");
...doesnt work.

















netsports

var value = 'hi there';
var RegExp = /^[A-Za-z0-9\-_]{6,16}$/;

if( RegExp.test(value) )
{
alert( 'yipee' );

}

of course best to limit the max number of chars as well as the min,
here it's 6 to 16.
 
S

shimmyshack

eval(charpos+1)+"]");
return false;
}//if
break;
}
...and i'm having trouble figuring how to add the added requirement of
"at least one numeric character" constraint.

oops just remove ^ and $

var value = 'hi th4ere';
var RegExp = /[A-Za-z0-9]{6,16}/;

if( RegExp.test(value) )
{
alert( 'yipee' );

}

this /does/ impose the requirement you are after.
 
L

Lee

shimmyshack said:
eval(charpos+1)+"]");
return false;
}//if
break;
}
...and i'm having trouble figuring how to add the added requirement of
"at least one numeric character" constraint.

oops just remove ^ and $

var value = 'hi th4ere';
var RegExp = /[A-Za-z0-9]{6,16}/;

if( RegExp.test(value) )
{
alert( 'yipee' );

}

this /does/ impose the requirement you are after.

How does that require at least one numeric character?


--
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
glegroups.com>, Thu, 19 Apr 2007 01:50:27, shimmyshack
var value = 'hi th4ere';
var RegExp = /[A-Za-z0-9]{6,16}/;

if( RegExp.test(value) )
{
alert( 'yipee' );

}

this /does/ impose the requirement you are after.


Does it enforce at least one character of each type? I think not.

One could do a \d test, and an [a-z] test, and AND the results; but
seeing that the OP seems only to allow alphanumerics then there must be
at least one of digit-letter & letter-digit present, so :

OK = /\d[a-z]|[a-z]\d/i.test(F.X0.value)

If _ is allowed, change [a-z] to \w and omit i. Test it more.

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.
 
S

shimmyshack

@o5g2000hsb.googlegroups.com:
eval(charpos+1)+"]");
return false;
}//if
break;
}
...and i'm having trouble figuring how to add the added requirement of
"at least one numeric character" constraint.

oops just remove ^ and $

var value = 'hi th4ere';
var RegExp = /[A-Za-z0-9]{6,16}/;

if( RegExp.test(value) )
{
alert( 'yipee' );

}

this /does/ impose the requirement you are after.


you seem to have more than one problem going on... using 'eval' is highly,
highly discouraged and quite possibly dangerous.

well I see what you are after now, sorry for the confusion - I think
you/ were clear - you must use something like

var value = 'hith9ere';
var RegExp = /^[a-zA-Z0-9]*[0-9]+[a-zA-Z0-9]*$/
//or
//var RegExp = /^[a-z0-9]*[0-9]+[a-z0-9]*$/i
//or
//var RegExp = /^\w*\d+\w*$/


if( RegExp.test(value) )
{
//ok
}

of course this allows 0 or more matches in the ranges a-z A-z 0-9
before at least one 0-9 followed by 0 or more matches from the larger
range again.
you could use the i modifier to reduce the complexity of the reg exp
slightly, or in further by using the \w* and \d+ character escapes
although \w includes the underscore character which you might not want
to allow. The 0-9 is repeated in the ranges because you might want
characters from the 0-9 to be repeated non-consecutively. sorry for
being needlessly sure of myself before!
 
S

shimmyshack

@o5g2000hsb.googlegroups.com:
eval(charpos+1)+"]");
return false;
}//if
break;
}
...and i'm having trouble figuring how to add the added requirement of
"at least one numeric character" constraint.
oops just remove ^ and $
var value = 'hi th4ere';
var RegExp = /[A-Za-z0-9]{6,16}/;
if( RegExp.test(value) )
{
alert( 'yipee' );

this /does/ impose the requirement you are after.

well I see what you are after now, sorry for the confusion - I think
you/ were clear - you must use something like

var value = 'hith9ere';
var RegExp = /^[a-zA-Z0-9]*[0-9]+[a-zA-Z0-9]*$/
//or
//var RegExp = /^[a-z0-9]*[0-9]+[a-z0-9]*$/i
//or
//var RegExp = /^\w*\d+\w*$/

if( RegExp.test(value) )
{
//ok

}

of course this allows 0 or more matches in the ranges a-z A-z 0-9
before at least one 0-9 followed by 0 or more matches from the larger
range again.
you could use the i modifier to reduce the complexity of the reg exp
slightly, or in further by using the \w* and \d+ character escapes
although \w includes the underscore character which you might not want
to allow. The 0-9 is repeated in the ranges because you might want
characters from the 0-9 to be repeated non-consecutively. sorry for
being needlessly sure of myself before!

just to clarify, you dont need the + after [0-9] because the * is
greedy of course, didnt spot that in time.
so var RegExp = /^\w*\d\w*$/ will do (or equivalent)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top