473,549 Members | 2,594 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 18173
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(val ue) )
{
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.val ue.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...@gma il.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(val ue) )
{
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.goo glegroups.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.co mwrote:
".Net Sports" <ballz2w...@cox .netwrote in news:1176950921 .045685.277030
@o5g2000hsb.goo glegroups.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(val ue) )
{
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.co mwrote:
>".Net Sports" <ballz2w...@cox .netwrote in news:1176950921 .045685.277030
@o5g2000hsb.go oglegroups.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(val ue) )
{
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...@gma il.comwrote:
On Apr 19, 5:28 am, Good Man <h...@letsgo.co mwrote:
".Net Sports" <ballz2w...@cox .netwrote in news:1176950921 .045685.277030
@o5g2000hsb.goo glegroups.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(val ue) )
{
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(val ue) )
{
//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...@gma il.comwrote:
On Apr 19, 9:50 am, shimmyshack <matt.fa...@gma il.comwrote:
On Apr 19, 5:28 am, Good Man <h...@letsgo.co mwrote:
".Net Sports" <ballz2w...@cox .netwrote in news:1176950921 .045685.277030
@o5g2000hsb.goo glegroups.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(val ue) )
{
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(val ue) )
{
//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.javas cript message <11************ *********@d57g2 000hsg.goo
glegroups.com>, Thu, 19 Apr 2007 01:50:27, shimmyshack
<ma********@gma il.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(val ue) )
{
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.val ue)

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.c om/faq/index.html>.
<URL:http://www.merlyn.demo n.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demo n.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
3255
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, just can't figure out how to implement that expression. Code snippets are most welcome. Best regards,
1
7394
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 that are in the comments should be kept intact. I've tried to write some Regexs, and I list them here for your information: regex = new...
2
2247
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
12627
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 seem to work in ASP.Net Any help please?
5
4462
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 expression validator expecting a message to be displayed but it let me submit the form. Do I have to use both?
2
1444
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 not contain the percentage symbol; AND (2) The field must not contain the asterisk symbol. It should contain neither of them. Can you please help...
3
5006
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 much-appreciated! Thanks!
4
1806
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 this field might be 089213 or 0895146. There will never be more than 4 characters, which follows that the total number of characters will not exceed 7....
1
1868
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 replaced to "escape V=".sp2" step 2: Later it has to be reversed as original(<escape V=".sp2" />) after some processing.
1
1567
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 consists of different instances like i for the first time ...
0
7965
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7483
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7817
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5375
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5092
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3504
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3487
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1063
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
771
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.