0

How to i get it rite ?? i tried the code below by it fail, anyone can correct me and tell when i when wrong ??

var Fname=document.getElementById('Fname').value;
if(!/^[A-z]*$/+$/.test(Fname)){
msg+='- Only Letter & Space '+email+'\n\n';}

2
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by darkraider
0

The RegExp you have there is incorrect. The correct one would be:

/*
For Letters: [a-zA-Z] (dealing with both upper and lower case characters)
For Space: [ ]
Putting them together: [a-zA-Z ]+
Use a + instead of * since a blank string is not a valid letter nor a space.
Use ^ and $ since our string should start and end with the given pattern.
*/
var myStr = "hello";
if(/^[a-zA-Z ]+$/.test(myStr) {
  window.alert("String contains only alphabets and spaces");
} else {
  window.alert("String doesn't contain only alphabets and spaces");
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.