RE to check, whether the string includes both letters and numbers

A

Anees

Hi,
how can i compile a user to enter both characters (a-z and A-Z) and
numbers while inputting a password field, with its length in between 8
and 14 using Regular expression
and the sting may also contain either _ or - with it

Please Help

Regards
Anees
 
T

Thomas 'PointedEars' Lahn

Anees said:
how can i compile a user

You can't, there are too much errors.
to enter both characters (a-z and A-Z) and numbers while inputting a
password field, with its length in between 8 and 14 using Regular
expression and the sting may also contain either _ or - with it

There must be an error in the Matrix. I have seen this very question
answered before.

http://jibbering.com/faq/


PointedEars
 
A

Anees

Hi Thomas
hw bt rdng th fq?
i went through the FAQ

and googled too
i tried this
/^[0-9a-zA-Z_-]{8,14}$/

But cant catch if anybody enters no numbers or vice versa..

please guide me

Anees
 
T

Thomas 'PointedEars' Lahn

Anees said:
i went through the FAQ

and googled too

Well, which query did you use?
i tried this
/^[0-9a-zA-Z_-]{8,14}$/

But cant catch if anybody enters no numbers or vice versa..

Given the string value `s', the obvious and most compatible solution is:

!/[^\da-z_-]/i.test(s)
&& /\d/.test(s)
&& /[a-z]/i.test(s)
&& s.length >= 8
&& s.length <= 14

But I also remember a way using one Regular Expression being posted here not
long ago. Googling for '(RegExp OR "Regular Expression") (character OR
letter OR number) length" should help.

Please try to use proper spelling next time.


PointedEars
 
D

Dr J R Stockton

how can i compile a user to enter both characters (a-z and A-Z) and
numbers while inputting a password field, with its length in between 8
and 14 using Regular expression
and the sting may also contain either _ or - with it

An obvious solution is:

OK = /^[\da-z_-]{8,14}$/i.test(s) && /\d.*[a-z]|[a-z].*\d/i.test(s)

The first RegExp checks the character set and length. The second
checks that either there is a digit and after that a letter, or /vice
versa/; to pass that requires at least one of each, in either order.
Tested, but only slightly.

Don't worry about Grumpy; he seems incurable.
 
E

Evertjan.

Dr J R Stockton wrote on 10 apr 2008 in comp.lang.javascript:
how can i compile a user to enter both characters (a-z and A-Z) and
numbers while inputting a password field, with its length in between 8
and 14 using Regular expression
and the sting may also contain either _ or - with it

An obvious solution is:

OK = /^[\da-z_-]{8,14}$/i.test(s) && /\d.*[a-z]|[a-z].*\d/i.test(s)

The first RegExp checks the character set and length. The second
checks that either there is a digit and after that a letter, or /vice
versa/; to pass that requires at least one of each, in either order.
Tested, but only slightly.

Don't worry about Grumpy; he seems incurable.

I would simply follow the specifications:

result = /\d/.test(s)&&/[a-z]/i.test(s)&&/^[\da-z_-]{8,14}$/i.test(s);
 
P

pr

Dr said:
how can i compile a user to enter both characters (a-z and A-Z) and
numbers while inputting a password field, with its length in between 8
and 14 using Regular expression
and the sting may also contain either _ or - with it

An obvious solution is:

OK = /^[\da-z_-]{8,14}$/i.test(s) && /\d.*[a-z]|[a-z].*\d/i.test(s)

The same but slightly shorter:

OK = /^[-\w]{8,14}$/.test(s) && /\d\D|\D\d/.test(s);
 
T

Thomas 'PointedEars' Lahn

pr said:
Dr said:
how can i compile a user to enter both characters (a-z and A-Z) and
numbers while inputting a password field, with its length in between
8 and 14 using Regular expression and the sting may also contain
either _ or - with it
An obvious solution is:

OK = /^[\da-z_-]{8,14}$/i.test(s) && /\d.*[a-z]|[a-z].*\d/i.test(s)

The same but slightly shorter:

OK = /^[-\w]{8,14}$/.test(s) && /\d\D|\D\d/.test(s);

This is _not_ the same as above. For your second expression to match, at
least one decimal digit has to be followed directly by its grammatical
opposite or vice-versa. There is no such requirement in the OP's
specification. Instead, you were looking for

OK = /^[-\w]{8,14}$/.test(s) && /\d.*\D|\D.*\d/.test(s);

However, /\D/i includes a lot more different characters than /[a-z]/i, and
therefore, although many of them are excluded by the previous test, is
probably less efficient than the latter.

Finally, I was about to say "Good catch about \w, though." Yet AIUI,
section 2 of the Specification indicates that /\w/ as a substitute for
/[0-9A-Z_]/i cannot be trusted.


PointedEars
 
P

pr

Thomas said:
pr said:
Dr said:
how can i compile a user to enter both characters (a-z and A-Z) and
numbers while inputting a password field, with its length in between
8 and 14 using Regular expression and the sting may also contain
either _ or - with it
An obvious solution is:

OK = /^[\da-z_-]{8,14}$/i.test(s) && /\d.*[a-z]|[a-z].*\d/i.test(s)
The same but slightly shorter:

OK = /^[-\w]{8,14}$/.test(s) && /\d\D|\D\d/.test(s);

This is _not_ the same as above. For your second expression to match, at
least one decimal digit has to be followed directly by its grammatical
opposite or vice-versa. There is no such requirement in the OP's
specification. Instead, you were looking for

OK = /^[-\w]{8,14}$/.test(s) && /\d.*\D|\D.*\d/.test(s);

Yes and no. An 8-character string that contains at least one digit and
at least one non-digit must contain \d\D|\D\d *somewhere*. It isn't a
correct expression, however, because it reports '1________' as OK,
whereas the OP required at least one alpha character. Dr Stockton's
version is right, AFAICS.
However, /\D/i includes a lot more different characters than /[a-z]/i, and
therefore, although many of them are excluded by the previous test, is
probably less efficient than the latter.

Finally, I was about to say "Good catch about \w, though." Yet AIUI,
section 2 of the Specification indicates that /\w/ as a substitute for
/[0-9A-Z_]/i cannot be trusted.

I have a vague memory of a browser once upon a time in a non-English
locale interpreting \w to include some local characters, but that is
likely to have been in the distant past. Section 15.10.2.12 of ECMA-262
is refreshingly exact about the characters that 'CharacterClassEscape ::
w' is intended to represent these days.
 
T

Thomas 'PointedEars' Lahn

pr said:
Thomas said:
pr said:
Dr J R Stockton wrote:
how can i compile a user to enter both characters (a-z and A-Z)
and numbers while inputting a password field, with its length in
between 8 and 14 using Regular expression and the sting may also
contain either _ or - with it
An obvious solution is:

OK = /^[\da-z_-]{8,14}$/i.test(s) && /\d.*[a-z]|[a-z].*\d/i.test(s)

The same but slightly shorter:

OK = /^[-\w]{8,14}$/.test(s) && /\d\D|\D\d/.test(s);
This is _not_ the same as above. For your second expression to match,
at least one decimal digit has to be followed directly by its
grammatical opposite or vice-versa. There is no such requirement in
the OP's specification. Instead, you were looking for

OK = /^[-\w]{8,14}$/.test(s) && /\d.*\D|\D.*\d/.test(s);

Yes and no. An 8-character string that contains at least one digit and at
least one non-digit must contain \d\D|\D\d *somewhere*.

You are right, I overlooked that.
It isn't a correct expression, however, because it reports '1________' as
OK, whereas the OP required at least one alpha character. Dr Stockton's
version is right, AFAICS.

ACK, and not using .* there is even more efficient.
However, /\D/i includes a lot more different characters than /[a-z]/i,
and therefore, although many of them are excluded by the previous test,
is probably less efficient than the latter.

Finally, I was about to say "Good catch about \w, though." Yet AIUI,
section 2 of the Specification indicates that /\w/ as a substitute for ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
/[0-9A-Z_]/i cannot be trusted.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
I have a vague memory of a browser once upon a time in a non-English
locale interpreting \w to include some local characters, but that is
likely to have been in the distant past. Section 15.10.2.12 of ECMA-262
is refreshingly exact about the characters that 'CharacterClassEscape ::
w' is intended to represent these days.

But did you consider section *2* as well?

| A conforming implementation of ECMAScript is permitted to support program
| and regular expression syntax not described in this specification. [...]

AIUI, this also means a conforming implementation may implement \w
differently than described in the Specification.


PointedEars
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected].
Dr said:
how can i compile a user to enter both characters (a-z and A-Z) and
numbers while inputting a password field, with its length in between 8
and 14 using Regular expression
and the sting may also contain either _ or - with it
An obvious solution is:
OK = /^[\da-z_-]{8,14}$/i.test(s) && /\d.*[a-z]|[a-
z].*\d/i.test(s)

The same but slightly shorter:

OK = /^[-\w]{8,14}$/.test(s) && /\d\D|\D\d/.test(s);

That accepts, for example, s = "3-----------".

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

pr

Thomas said:
pr said:
Thomas said:
pr wrote:
Dr J R Stockton wrote:
how can i compile a user to enter both characters (a-z and A-Z)
and numbers while inputting a password field, with its length in
between 8 and 14 using Regular expression and the sting may also
contain either _ or - with it
An obvious solution is:

OK = /^[\da-z_-]{8,14}$/i.test(s) && /\d.*[a-z]|[a-z].*\d/i.test(s)
[...]
Dr Stockton's version is right, AFAICS.

ACK, and not using .* there is even more efficient.

You can't remove .* from Dr Stockton's expression, because the digit may
be separated from the alphabetical character by one or more (or no) '_'
or '-'.

[...]
Finally, I was about to say "Good catch about \w, though." Yet AIUI,
section 2 of the Specification indicates that /\w/ as a substitute for ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
/[0-9A-Z_]/i cannot be trusted.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
I have a vague memory of a browser once upon a time in a non-English
locale interpreting \w to include some local characters, but that is
likely to have been in the distant past. Section 15.10.2.12 of ECMA-262
is refreshingly exact about the characters that 'CharacterClassEscape ::
w' is intended to represent these days.

But did you consider section *2* as well?

| A conforming implementation of ECMAScript is permitted to support program
| and regular expression syntax not described in this specification. [...]

AIUI, this also means a conforming implementation may implement \w
differently than described in the Specification.

The way I read it, the first paragraph is the most important:

| A conforming implementation of ECMAScript must provide and support
| all the types, values, objects, properties, functions, and program
| syntax and semantics described in this specification.

So it must provide regular expressions and the '\w' escape as described.

| A conforming implementation of ECMAScript is permitted to provide
| additional types, values, objects, properties, and functions beyond
| those described in this specification.

and

| A conforming implementation of ECMAScript is permitted to support
| program and regular expression syntax not described in this
| specification.

I take to mean that the implementation may supply *additional* objects,
properties, syntax, etc. over and above what is described. So '\w' must
conform to its description, but there is no reason why an implementation
can't introduce a '\q' sequence that matches quote marks (say), or
lookbehinds, or some wholly new and imaginative property of the RegExp
object.

That would just be my opinion, however.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top