...

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



pml
04-11-2005, 08: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; }
else
{ return true; }
}

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

Kor
04-11-2005, 09:06 AM
Try regexp:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<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,"");
}
}
</script>
</head>
<body>
<input name="" type="text" onkeyup="valid(this)">
</body>
</html>

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

Kor
04-11-2005, 10: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,"");



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum