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

Regular expression for required alpha and numeric characters

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

Apr 18 '07 #1
8 18165
On Apr 18, 10:13 pm, ".Net Sports" <ballz2w...@cox.netwrote:
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.

Apr 18 '07 #2
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

On Apr 18, 3:20 pm, shimmyshack <matt.fa...@gmail.comwrote:
On Apr 18, 10:13 pm, ".Net Sports" <ballz2w...@cox.netwrote:
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.

Apr 19 '07 #3
".Net Sports" <ba********@cox.netwrote in news:1176950921.045685.277030
@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.
you seem to have more than one problem going on... using 'eval' is highly,
highly discouraged and quite possibly dangerous.
Apr 19 '07 #4
On Apr 19, 5:28 am, Good Man <h...@letsgo.comwrote:
".Net Sports" <ballz2w...@cox.netwrote in news:1176950921.045685.277030
@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.

Apr 19 '07 #5
Lee
shimmyshack said:
>
On Apr 19, 5:28 am, Good Man <h...@letsgo.comwrote:
>".Net Sports" <ballz2w...@cox.netwrote in news:1176950921.045685.277030
@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.
How does that require at least one numeric character?
--

Apr 19 '07 #6
On Apr 19, 9:50 am, shimmyshack <matt.fa...@gmail.comwrote:
On Apr 19, 5:28 am, Good Man <h...@letsgo.comwrote:
".Net Sports" <ballz2w...@cox.netwrote in news:1176950921.045685.277030
@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!

Apr 19 '07 #7
On Apr 19, 6:24 pm, shimmyshack <matt.fa...@gmail.comwrote:
On Apr 19, 9:50 am, shimmyshack <matt.fa...@gmail.comwrote:
On Apr 19, 5:28 am, Good Man <h...@letsgo.comwrote:
".Net Sports" <ballz2w...@cox.netwrote in news:1176950921.045685.277030
@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!
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)

Apr 19 '07 #8
In comp.lang.javascript message <11*********************@d57g2000hsg.goo
glegroups.com>, Thu, 19 Apr 2007 01:50:27, shimmyshack
<ma********@gmail.composted:
...and i'm having trouble figuring how to add the added requirement of
"at least one numeric character" constraint.
>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.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Apr 19 '07 #9

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

Similar topics

2
by: Mickel Grönroos | last post by:
Hi Pythoneers, Which is the best way of checking that a given unicode string only contains IPA characters, e.g. characters in the range \u0250-\u02AF? I guess a regular expression would do it,...
1
by: Laser Lu | last post by:
Hi, all, I'm now writing a program to compress JavaScript code. One puzzle is how to write a regular expression to find out and remove all the redundent blank spaces. However, those blank spaces...
2
by: Nazir | last post by:
Hi I'm using a regular expression validator, but if spaces are entered, it bypasses the validation! I'm using ^{5,100}$
2
by: RJN | last post by:
Hi I want to use regularexpression validator for a text box . I want the entered value to have only alphanumeric , space, underscrore and - In VB I used something like this. . Same doesn't...
5
by: John . | last post by:
I am using the Regular Expression Validator control to validate a correct email address. But, at the same time I would like to make it a required field. I tested by using just the regular...
2
by: Sathyaish | last post by:
RegEx heros, I want to validate a certain field such that it shouldn't contain the percentage character (%) or the asterisk character symbol (*). The specification is that: (1) The field must...
3
by: sianan | last post by:
I am looking for a regular expression to validate input into a textbox. I only want to allow valid alpha-numeric characters (no special characters, such as ?, *, {} etc...). An example would be...
4
by: CrazyCruzan | last post by:
Hello, I have a table that has a string field "ANUM" that has values that always begin with 089. Then the 3 characters; 089, are followed by between 3 or 4 characters. So a typical value in...
1
by: =?Utf-8?B?R3VoYW5hdGg=?= | last post by:
I have a string with some escape charaecters that need to be processed in our file Say for example the string might have something like following <escape V=".sp2" /> step 1: This has to be...
1
by: koduruabhinav | last post by:
Hi, some simple code required.i was trying but i was unable. But i want instances to get printed each time like we are printing comparing instance name. suppose if we take Each X_RAMB18E1 module...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.