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';}

Recommended Answers

All 2 Replies

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");
}

ok will go try it out thk man :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.