hi everyone. i am having trouble with validating an input field where someone is supposed to enter their full name. i got this validation from the internet but it does not allow spaces between words. please help me correct it and explain to me what was wrong. Thanks a bunch.

the code i have is

var alphaExp = /^[0-9a-zA-Z]+$/;
		
if(elem.value.match(alphaExp)) {
	return true;
}else{
       alert("Enter name please");
}

hiii
i have got no idea about reg exp
but this code can help you achieve the same thing

function charOnly()
 {
  
  if( (event.keyCode >=65 && event.keyCode <= 90 ) || (event.keyCode >=97 && event.keyCode <= 122) || (event.keyCode == 47 ))
	{
	  event.returnValue=true;
 	}
  else
	{
	 event.returnValue=false;
	}

}

i used this for alphabets and "/" .
U can look up the ASCII for space and add it instead of "47"...

u can call it on the keypress events..

hope it helps...


PS: this function lets you enter only "Characters" , it doesn't validate for an empty field,
for that you can use

if(document.getElementById("elementname").value="")
 {
  alert("enter name")
  form.elementname.focus();
  return false;
 }

Go with this simple pattern /^[a-zA-Z\.\s]+$/; , and also excluding the use of digits in the field. This pattern will allow the user to enter character's from A-Z, a-z including dots and white spaces.


Hope it helps...

Here's a simple field name validation that will force the user to enter his/her name starting in a capital letter, and also not allowing them to enter numeric value's with their names.

All codes is as follows:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html id="html4" lang="en">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<title>Live Help!</title>

<script type="text/javascript">
<!-- 
var validNames = /^[A-Z]([a-z]+(|\s[a-zA-Z\.]+)$)/;
var validate = function( form ) {
   
   var nam = form.names.value;
   var nonsingle = (( nam.match(/[A-Z](?=[A-Z]+)/g)) ? " starts on a single " : " starts with a " );
   if ( validNames.test( nam )) {
   alert("Thank you " + nam + ", for visiting my site...");
   return true;
   }   
   if ( nam.match(/\d+/g) ) {
   alert("Only letters from Aa to Zz are allowed on this field.");
    form.names.focus();
    return false;
    } alert("You must enter a name that" + nonsingle + "capital letter." );
   form.names.focus();
   return false;
}; 
// -->
</script>
</head>
<body>
<div id="main">
<form id="myform" name="myform" action="#" onsubmit="return validate( this );">
<div><label for="names">Name: <input type="text" id="names" name="names" value="" size="30"></label> <input type="submit" value="Submit"></div>
</form>
</div>
</body>
</html>
This article has been dead for over six months. Start a new discussion instead.