views:

69

answers:

6

Sample data: Hello I'm 301

I need a regex to allow A-Z a-z 0-9 and space(s) only.

All other characters are not allowed. If detected, return false to javascript.

Based on the sample data above, it should return false because got a character which is not accetable===> '

How to write this in js.

+1  A: 

You'll probably want to use: /[A-Za-z0-9 ]*/.test(someInputString)

--edit: as noted in comments and other answers, the regex should be /^[A-Za-z0-9 ]*$/

Romain
I'm not sure that will work as the asker wanted. That will return true if there are any valid characters in the string, not if the string is composed entirely of valid characters.
Jason Musgrove
Exact! I edited my answer accordingly. :)
Romain
A: 

You need to create the Javascript Regex object first.

var MyRegex = new RegExp(/^[a-zA-Z0-9\s]+$/)

or simply

var MyRegex = /^[a-zA-Z0-9\s]+$/

You can then use this to test, which will return a boolean

var MyString = "Hello I'm 301"
if (MyRegex.test (MyString)) 
{
    // Would be true here
}
else
{
    // Would be false here
    // Would fall through to here due to '
}

I believe you can also do /^[a-zA-Z0-9\s]+$/.text(MyString)

Xetius
`.test()` that is :)
jensgram
+1  A: 

Try this

function check(s){
    return /^[A-Za-z0-9 ]+$/.test(s);
}
S.Mark
+2  A: 

I suggest the regex:

/^[A-Z0-9 ]+$/i.test(someinput);

This ensures that the input ONLY consists of the characters mentioned in the regex, by "anchoring" the regex from start-of-the-string (indicated by "^") until the end of string ("$"). The trailing "/i" on the regex makes it a case-insensitive match, relieving specification of both cases of the letters.

haavee
Shouldn't it be `^[A-Z0-9 ]+$` (with the plus, to signify one or more of the preceding character class)?
J-P
of course ... you are absolutely right!
haavee
A: 

Try this:

    var x = 'Hello I\'m 301';
    var z = x.match(/^[A-Za-z0-9\s]+$/g);
    alert(z);
    x = 'Hello Im 301';
    var y = x.match(/^[A-Za-z0-9\s]+$/g);
    alert(y);

Don't forget to escape the single quote in the test string :)

You can return true/false by checking for null after the regex match, if the sample string fails the match then the result is null.

slugster
+2  A: 

Any string in Javascript has a match() function that accepts a regex and returns null if it doesn't match. For instance, if you have:

var s = "Hello I'm 301";

you can test it with:

if (s.match(/^[a-z0-9 ]*$/i)
 alert("string is ok!");
else
 alert("string is bad!");

On to the regex: /^[a-z0-9 ]*$/i

The caret(^) at the beginning and the dollar ($) at the end are anchors. They mean "beginning of string" and "end of string". They force the regex to cover the entire string, not just portions. The square brackets define a character range: letters and numbers, and also space.

Without the caret and the dollar (the anchors), your regex would have matched any valid character and would have returned true.

The final "i" is a regexp option, meaning "case insensitive".

Hope that helps!

Vlad Valceanu