View Full Version : Validating first/last names without numbers and symbols?

04-11-2005, 09:45 AM
I have two text boxes in my form where the user should enter the first and last names. I want to check that neither of the boxes contain numbers or different symbols. The only allowed characters in both cases are letters, hyphens("-") and spaces(" "). Any other symbols but these three mentioned should generate false. Can anyone help me check for that?

function check(){

if(document.form1.text1.value=="??????"){ alert("Your first name must not contain numbers or symbols"); return false; }
else if (document.form1.text2.value=="????????"){ alert("Your last name must not contain numbers or symbols"); return false; }
{ return true; }

<form name=form1 onsubmit=”check()”>

04-11-2005, 10:06 AM
Try regexp:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/JavaScript">
function valid(field) {
var re = /^[A-z-' '-'-']*$/;
if (!re.test(field.value)) {
alert("Only letters, space and '-' character are allowed!");
field.value = field.value.replace(/[^A-z-' '-'-']/g,"");
<input name="" type="text" onkeyup="valid(this)">

Philip M
04-11-2005, 10:48 AM
What about Messrs O'Reilly, De'Ath and so on?

04-11-2005, 11:18 AM
:D Yea, I have not thought about (neither pml, as she/he did not specify that). maybe it her/his country (as in mine as well) there are no such names...

that's simple

var re = /^[A-z-' '-'-'-''']*$/;
field.value = field.value.replace(/[^A-z-' '-'-'-''']/g,"");