Reputation: 1282
Can any one give the regular expression for checking a string
which must contain a space and be alpha numeric.
Upvotes: 2
Views: 6279
Reputation: 4145
In JavaScript you can solve it like this:
if (/^[a-z\d]* [a-z\d]*$/i.test(input)) {
// Successful match
} else {
// Fail
}
Notes:
i
following the regex means
ignore case, i.e. it matchez a-z as
well as A-Zinput
+
instead of *
Upvotes: 4
Reputation: 900
I'm assuming you want the space to be between two alpha numeric substrings. I have also added \s*
to allow for whitespace encasing the alpha-numerics (which judging by your comment is what you actually want)
^\s*[0-9a-zA-Z]+[ ][0-9a-zA-z]+\s*$
Upvotes: 2
Reputation: 59287
/^[A-Za-z0-9]+ [A-Za-z0-9]+$/
If the space can be at the end or the beginning, change the correspondent +
to *
.
Upvotes: 0