-1

I have some code to validate a registration form, but it's really basic.

How can I change both the fname and lname to be greater than 3 characters but less than 12?

<script>
function validateForm()
{
//first name
var x=document.forms["registerForm"]["fname"].value;
if (x==null || x=="")
  {
  alert("First name must be filled out");
  return false;
  }

//last name
var x=document.forms["registerForm"]["lname"].value;
if (x==null || x=="")
  {
  alert("Last name must be filled out");
  return false;
  }

 //email 
var x=document.forms["registerForm"]["email"].value;
var atpos=x.indexOf("@");
var dotpos=x.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
  {
  alert("Not a valid e-mail address");
  return false;
  }
}
</script>
Sammitch
  • 30,782
  • 7
  • 50
  • 77
Elaine Adams
  • 179
  • 10
  • 1
    add what about people with names shorter than 3 and longer than 12 ? –  Mar 20 '14 at 20:39
  • I don't care for such people lol. – Elaine Adams Mar 20 '14 at 20:40
  • honestly though, i need some boundaries. I cant imagine there are many people called Si Tu – Elaine Adams Mar 20 '14 at 20:42
  • but why any boundaries at all? there are millions of people with 2 letter first names. http://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/ –  Mar 20 '14 at 20:42
  • You should not redefine `x`, since it's already been defined in the same scope. – M Miller Mar 20 '14 at 20:43
  • Li (Wade trans) is the 2nd most common Chinese surname –  Mar 20 '14 at 20:46
  • 2
    This is the official website for people with names of more than 2 letters but less than 12 (www.PeopleWithNamesMoreThan2CharactersButLessThan12.com) You didn't know the scope of my project. – Elaine Adams Mar 20 '14 at 20:46

2 Answers2

1

Use the length property of a string.

var fname = document.forms['registerForm']['fname'].value,
    lname = document.forms['registerForm']['lname'].value;

if (fname.length <= 3 || fname.length >= 12) {
   alert('Your first name must be between 4 to 11 characters.');
}

if (lname.length <= 3 || lname.length >= 12) {
   alert('Your last name must be between 4 to 11 characters.');
}
M Miller
  • 5,364
  • 9
  • 43
  • 65
  • Thanks for a serious response. It works. Do you also know how to ensure an accurate DOB is entered? (DD/MM/YYYY) – Elaine Adams Mar 20 '14 at 21:01
  • Not as easy as it sounds if you want to check, say, if there were 29 days in February in a leap year, etc. But you can start here: http://stackoverflow.com/questions/10194464/javascript-dd-mm-yyyy-date-check – M Miller Mar 20 '14 at 21:08
  • Ah, i've just looked over it. Looks tricky! I'm guessing validating a postcode (british) is just as tricky? – Elaine Adams Mar 20 '14 at 21:14
  • Google is your best friend: http://stackoverflow.com/questions/13969461/javascript-uk-postcode-validation – M Miller Mar 20 '14 at 21:42
0

try this-

if (x.length<3 || x.length>12)
  {
  alert("name length is not correct");
  return false;
  }
sunny
  • 1,156
  • 8
  • 15