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;
       alert("Enter name please");
7 Years
Discussion Span
Last Post by essential

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 ))


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

  alert("enter name")
  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"
<html id="html4" lang="en">
<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.");
    return false;
    } alert("You must enter a name that" + nonsingle + "capital letter." );
   return false;
// -->
<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>
This article has been dead for over six months. 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.