Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    pml
    pml is offline
    New Coder
    Join Date
    Mar 2005
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Validating first/last names without numbers and symbols?

    I have two text boxes in my form where the user should enter the first and last names. I want to check that neither of the boxes contain numbers or different symbols. The only allowed characters in both cases are letters, hyphens("-") and spaces(" "). Any other symbols but these three mentioned should generate false. Can anyone help me check for that?


    Code:
    function check(){ 
    
    if(document.form1.text1.value=="??????"){ alert("Your first name must not contain numbers or symbols"); return false; } 
    else if (document.form1.text2.value=="????????"){ alert("Your last name must not contain numbers or symbols"); return false; }
    else
    { return true; }
    }
    
    <form name=form1 onsubmit=”check()”>

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Try regexp:
    PHP Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
    <
    html>
    <
    head>
    <
    title>Untitled Document</title>
    <
    meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <
    meta http-equiv="Content-Style-Type" content="text/css">
    <
    meta http-equiv="Content-Script-Type" content="text/javascript">
    <
    script type="text/JavaScript">
    function 
    valid(field) {
    var 
    re = /^[A-z-' '-'-']*$/;
    if (!
    re.test(field.value)) {
    alert("Only letters, space and '-' character are allowed!");
    field.value field.value.replace(/[^A-z-' '-'-']/g,"");
    }

    </script>
    </head>
    <body>
    <input name="" type="text" onkeyup="valid(this)">
    </body>
    </html> 
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    What about Messrs O'Reilly, De'Ath and so on?

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Yea, I have not thought about (neither pml, as she/he did not specify that). maybe it her/his country (as in mine as well) there are no such names...

    that's simple

    ...
    var re = /^[A-z-' '-'-'-''']*$/;
    ...
    field.value = field.value.replace(/[^A-z-' '-'-'-''']/g,"");
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •