...

View Full Version : validate that name has 2 names



esthera
01-22-2009, 06:55 PM
I have a name field -I want to validate that there is a space in the field - that the user has put in both firstname lastname and not just one

can someone help me with this?

itsallkizza
01-22-2009, 07:23 PM
Here ya go:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Example</title>
<style type="text/css">
</style>
<script type="text/javascript">
// <![CDATA[

function validateMe(form_elm)
{
var full = form_elm.full_name.value;
// clear any spaces in front
while (full.charAt(0) == " ") full = full.substr(1);
// split and check for two names - dual purpose of also retrieving first and last name for future use
full = full.split(" ");
var first_name = full[0];
var last_name = full[1];
if (full.length < 2) alert("Please enter both your first and last name.");
else alert("Thank you "+first_name+" "+last_name+".");
return false;
}

// ]]>
</script>
</head>
<body>

<form onsubmit="return validateMe(this)">
<input type="text" name="full_name" /><br />
<input type="submit" value="submit" />
</form>

</body>
</html>

Philip M
01-22-2009, 08:13 PM
This script checks that the field contains two names, with a least two alpha (only) characters in the first name and three alpha (only) characters in the last name, allowing for hyphens and/or apostrophes in the name e.g. Smith-Jones, O'Reilly; strips any leading or trailing spaces; removes multiple spaces between the names; capitalises the first letter of each word as required in English, with remaing letters in lowercase.



<input type = "text" name = "fullname" id = "fullname" onblur = "chkFullName()">

<script type = "text/javascript">
function chkFullName() {
var full = document.getElementById("fullname").value;
full = full.replace(/^\s+|\s+$/g,"") ; // strip any leading or trailing spaces
full = full.replace(/\s+/g," "); // remove multiple intermediate spaces
full = full.toLowerCase().replace(/\b[a-z]/g,function(w){return w.toUpperCase()}); // capitalise first letter of each name
if (!/(^[A-Za-z]{2,}\s[A-Za-z-']{3,}$)/.test(full)) { // first and second names separated by a space, allow for hyphen and apostrophe
alert ("You must enter your first name (not just your initial) and last name separated by a space");
document.getElementById("fullname").focus();
return false;
}
else {alert ("Thank you " + full) }
}
</script>




When trouble arises and things look bad, there is always one individual who perceives a solution and is willing to take command. Very often, that individual is crazy. Look out! He's coming!

Philip M
01-22-2009, 10:09 PM
This post refers to a post by another person which has been deleted by the Moderators.


Capitalizing the first letter of the names is meaningless for databases -- it isn't recognized.

Who mentioned a database?

You did not strip out any excess intermediate spaces, so
Cran space space Ford is invalid.
While C F-d and C F'd are both valid (the OP wanted firstname, not just initial). Hardly better than Cran Ford---
Cran O'Ford-Midwest with both an apostrophe and a hyphen returns false, but true in my regex.

So overall, not really a very great improvement, nicht wahr?
:p:p


"He was too clever by half. We all thought he would either end up in the Cabinet or in prison. We never dreamt he would do both."

Philip M
01-23-2009, 08:30 AM
This post refers to a further post by the same person which has been deleted by the Moderators.


Test code, before you run your mouth. Never mind, you'll never be able to extend that simple courtesy to anyone.

Funny, I did test the code:-


<script type = "text/javascript">
var nUser = "Cran Ford";
if (!/^\s*[a-z]+\s{1}[a-z]+[-']{0,1}[a-z]+\s*$/i.test(nUser)) {
alert ("Keep taking the pills!")
}
</script>

A first name is a name, not an initial. So minimum two characters (Al or Jo).

esthera - if you want to take account of Cranford's objections, you can add the following three lines to the code I provided:-


full = full.replace(/['-]+$/g,"?"); // no apostrophe or hyphen at end
full = full.replace(/-.*-/g,"?"); // only one hyphen allowed
full = full.replace(/'.*'/g,"?"); // only one apostrophe allowed

Personally I would not bother. There is virtually no way to stop some clown entering his name as Xx Zzz. You can ensure that each name contains one vowel and one consonant (Byng or Lee), and you can erase rude words (but be careful - Pen-istone and Scun-thorpe without the hyphens are English towns). But at the end of the day you cannot prevent some joker from entering his name as Cranford Gobonk-Yourself. :D:D

barkermn01
01-23-2009, 10:13 AM
why would you make its so hard for him.



function nameCheck(text){
name = text.split(' '); // make and array with [1] = fname [2] = ' ' [3] = lname
if(name[3] == ''){
alert('You Name is missing the last name');// tells them
return false; // prevent form from submitting
}else{
return true;
}
}


This is to be used as onSubmit in form tag used by 'nameCheck(docment.getElementByID('name').value);'

Nice and simple. ok lacking in security but nice and simple

Philip M
01-23-2009, 11:10 AM
why would you make its so hard for him.



function nameCheck(text){
name = text.split(' '); // make and array with [1] = fname [2] = ' ' [3] = lname
if(name[3] == ''){
alert('You Name is missing the last name');// tells them
return false; // prevent form from submitting
}else{
return true;
}
}


This is to be used as onSubmit in form tag used by 'nameCheck(docment.getElementByID('name').value);'

Nice and simple. ok lacking in security but nice and simple

Another "nice and simple" script posted without testing. It does not work.

I don't understand why people post scripts which are so obviously defective and cobbled together without any attempt to test them. This can only lead to frustration and disappointment for OPs who are beginners. In spite of Cranford's ludicrous comments, I always test my scripts before posting them (which is not to say that they are incapable of improvement).


If ALL that is wanted is to check that a space is entered (anywhere) into the input field, then:


<script type = "text/javascript">
function chkFullName() {
var full = document.getElementById("fullname").value;
if (!/\s/.test(full)) {
alert ("You must enter first name and last name");
return false;
}
}
</script>

It seem me that such a "validaton" is barely worthy of the name. At the very minimum you need to strip leading and trailing spaces so that one or more spaces alone do not return true.

itsallkizza
01-23-2009, 04:02 PM
I think the solution is obvious: Have the form e-mail you each individual name. You can then spend your day making sure each name is full, polite and plausible before entering them into your system :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum