...

View Full Version : Regexp not working ?



Mick.Dundee
06-16-2008, 02:30 PM
I have now a regexp



var stringExp = new RegExp(/\W/);

if (!stringExp.test(document.getElementById(inputboxid).value)) {

}
else {

}


This is working and I can fill in a till z / A till Z and _
However I want to have a till z / A till Z and _ - and space.

I got this regexp



var stringExp = new RegExp(/^[a-zA-Z]+l{2}[a-zA-Z\s]*$/);


But this aint working ...

Philip M
06-16-2008, 02:47 PM
Not sure exactly what you want:-

var stringExp = new RegExp(/[^\w\s\-]/); // match anything NOT a-z A-Z 0-9 underscore space hyphen


If you do not want numerals

var stringExp = new RegExp(/[^a-z\s\-\_]/i); // match anything NOT a-z A-Z space hyphen underscore

Mick.Dundee
06-16-2008, 03:09 PM
Euhm what I don't want is that users can fill in

&^%$ (special characters) etc
Only numbers, space, _ - , letters (a-z / A-Z) and .

Philip M
06-16-2008, 04:36 PM
Euhm what I don't want is that users can fill in

&^%$ (special characters) etc
Only numbers, space, _ - , letters (a-z / A-Z) and .

I don't see any mention of . (dot) in your first post.

var stringExp = new RegExp(/[^\w\s\-\.]/); // match anything NOT a-z A-Z 0-9 underscore space hyphen period

i. e. returns true if any other character is present in the string, in which case the field is invalid and should be re-entered by the user.

\w matches any alpha-numeric character including the underscore. It is equivalent to [a-zA-Z0-9_]
^ within the square brackets means "not".

if (stringExp.test(document.getElementById(inputboxid).value)) { // fail the test if true

You may think it a good idea to strip leading spaces (prevent all spaces entered in the field):-

x = document.getElementById(inputboxid).value;
x = x.replace(/^\s+/,""); // strip leading spaces
if (stringExp.test(x)) { // fail the test if true

Always go to other people’s funerals, otherwise they won’t come to yours.

Mick.Dundee
06-17-2008, 09:55 AM
Mhhh tried solutions but can't get it to work.

To clarify my start post:

I want to check the username on valid input.

Valid usernames are

Firstname.Lastname
Firstname Lastname
Firstname_Lastname
Firstname-Lastname
Firstname
Lastname

And the username may be letters (a till z) or numbers (0-9)

Illegal characters (on every position) are for example

!@#$%^&*()+,?<>

So some examples of illegal usernames

First@Name Lastname
FirstName' Lastname
%
#Lastname
'
Firstname in '

etc

Philip M
06-17-2008, 11:49 AM
Well, it works fine for me.

You can test the regex thus:-


<input type = "text" id = "inputboxid" size = "20" onblur = "chk()">

<script type = "text/javascript">
function chk() {
inp = document.getElementById("inputboxid").value
if (/[^\w\s\-\.]/.test(inp)) { // i.e. if an invalid character is present in the string
alert ("Invalid")
}
else {
alert ("OK")
}
}
</script>


What about people who have apostophes in their names (O'Reilly etc.)?


You can test your regular expressions at: http://www.claughton.clara.net/regextester.html

Mick.Dundee
06-18-2008, 08:56 AM
Very strange, I tried again (first in a clean .html file) and it works.
Then I changed again my .JS file and it works :S

Maybe cached JS file ?

Good question, will have think about it because I won't want those names showing as invalid :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum