0

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

2
Contributors
10
Replies
50
Views
4 Years
Discussion Span
Last Post by pritaeas
0
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....

Edited by Jane160768

0

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.

0

@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?

0

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

Edited by Jane160768

0

@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;


}
0

@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 + '"');
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.