I need to validate that my textbox contains a first and last name. That it only has alphabet letters and upper and lower case. I know normally names could have other charaters but for this purpose I need them only to have letters. Regular expression can not be used.... If it has ex: John 1235 has to return false.... if it has john smith jr can return true....

Can some one point me in the right direction on how to achieve this please

Thanks in advance

Jane

function CardHolder(name) {   

  var alphabet = 'abcdefghijklmnopqrstuvwxyz';
  alphabet = alphabet + ' ' + alphabet.toUpperCase();
  alphabet = alphabet.split(''); 
  for (i=0; i<name.length; i++) {
    if (!~alphabet.indexOf(name.charAt(i))){  
       return false;

  }else{

  return true;
}
  }

}

If i put in a name ect Jane Doe it returns true..... but if i put in Jane 1696 it still returns true... if i put in janedoe it returns true but i need it to have space and capital for first letter of name and last name... can someone help me please....

You don't need the else, it will break after the first successful character. Return true at the end of the function, after the loop.

@pritaeas Thank you have done that. Do you know how I can insure that the first of each name is a capital and have a space between them?

You need to keep track of i == 0 for the first character, and the character after the space.

@pritaeas Thank you but am not sure how to do that for the second part of the name

Store the position of the space in a variable, the next one should be caps.

@pritaeas I have been having trouble.... not really understanding it... Can you try and explain it to me please. This is what I have, but if i put a first name ex JOHN it comes up true, when I put in JOHN SMITH it comes up false. lol I need it around the other way....

function isValidCardHolder(name) {


    var letters = 'abcdefghijklmnopqrstuvwxyz';
    letters  = letters + ''  + letters.toUpperCase();
    letters = letters.split('');
    for (i = 0; i < name.length; i++) {  
    if (!~letters.indexOf(name.charAt(i))){
        return false;   
         }

         for (j = 1; j<name.length; j++){
             if (!~letters.indexOf(name.charAt(j))){
        return false;           
             }


       }
     } 
         return true;


}

@pritaeas ok still trying to work it out lol please can someone help me , it is driving me crazy....

function isValidCardHolder(name) {

    var str = ' ';
    var str2 = str.trim();

    for (i = 0; i < name.length; i++) {
        if (str.charCodeAt(i)>=97 &&str.charCodeAt(i)<=122 || (str.charCodeAt(i)==32)) {
            return true;

        }else if (str.charCodeAt(0)==32 || str.charCodeAt(str.length-1)==32){
            return false;

           }else if(name.lastIndexOf(' ')==-1 ){ // check to make sure space


   return false;
    } 
    }return true;
    alert('"' + str2 + '"');
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.