javascript - How to capture content of an input element and test if it is characters betwween letters a-z A-Z with a space berween two words -


i have this code:

$(document).ready(function(){     $("input").focus(function(){         $(this).css('outline-color','#559fff');         $(this).blur(function(){             $(this).css("outline-color","#ff0000");         });     });     $("input").click(function(){      var value = $(this).val(function(){          $(this).html("");       });     });     $(".awesome").click(function(){         var tostore = $("input[name=name]").val();         if((tostore===/[a-za-z]/)===true){         $("#contain").children().fadeout(1000);         $("#contain").delay(5000).queue(function(){             $("#contain").append("<p>welcome : " + tostore + " </p>");     });         }else{             alert("you must put valid name");         } }); }); 

i want code test , catch value of input , if value characters between a-z including capitalize space between 2 words like: "firstname lastname" if ok thne procced to:

$("#contain").children().fadeout(1000);         $("#contain").delay(5000).queue(function(){             $("#contain").append("<p>welcome : " + tostore + " </p>");     }); 

else alert user must put valid characters.

i think regex should work:

if (/^[a-za-z]+ [a-za-z]+$/.test(tostore)) {  } 

and should put in place of if((tostore===/[a-za-z]/)===true){

demo: http://jsfiddle.net/tdvwk/

this checks input follows this:

  • starts 1 or more alphabetic characters (any can uppercase or lowercase)
  • contains space after previous set of characters
  • ends 1 or more alphabetic characters (any can uppercase or lowercase)

if want more strict , require each name start uppercase letter , rest lowercase, can use:

if (/^[a-z][a-z]? [a-z][a-z]?$/.test(tostore)) {  } 

but isn't ideal, names different , "mclovin"...where second example fail. first example should complete need.

of course, there's debate shouldn't restrict much. if name more first , last? if have suffix, "iii" (or "3"), designating third of family name? if people want include middle name (on purpose or accident)? might make more sense use 2 textboxes each name, making more clear user. way, have validate each filled in (and maybe has alphabetic characters). again, i'm not sure requirements , textbox have :)


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -