Username should take alphabets

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • meeanji
    New Member
    • May 2008
    • 7

    Username should take alphabets

    Hi friends, i need to write validation for textbox(Usernme ) which should accept only alphabets. I am able throw validation that Please fill the UserName Details.Now it should throw that it accepts only alphabets.My code goes like this::

    Code:
    <tr>
    	   <td style="width:30%" align="right">User Name :</td>
    	   <td style="width:70%"> <input name="username" size="20" maxlength="155" value="<?php echo $username;?>"   onfocus="setVisibility('100','inline')"; onBlur="setVisibility('100','none')"> </td>
    	 </tr>
    <tr>
    	   <td align="right">Password :</td>
    	   <td> <input name="password" type="password" size="20" maxlength="155" value="<?php echo $password;?>"   onfocus="setVisibility('100','inline')"; onBlur="setVisibility('100','none')"> </td>
    	     
    	   </tr>
    
    
    <script type="text/javascript">
    
    
    function validate_form ( )
    {
        valid = true;
        
        if ( document.msgform.username.value == "" )
        {
            alert ( "Please fill the UserName Details." );
            valid = false;
        }
        else if(document.msgform.password.value == "")
    	{
    	 alert ( "Please fill the Password Details." );
            valid = false;
    	}
    return valid;
    	
    }	
    	
    <td>
    	 <td>  <input type="submit" name="submit" value="Submit"  onClick="return validate_form();"></td></div>
    	   </td>
     
    
    </script>
    Please can anybody tellme how to write validations for this username that it should accept only Alphabets.

    Regards,
    meeanji
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    You could use a regular expression, e.g. /^[a-z]+$/i You can use the match method of the string to test if it matches the regular expression, e.g.
    [code=javascript]var username = document.msgfor m.username.valu e;
    if (!username.matc h(/^[a-z]+$/i) {
    // you can alert here...
    [/code]

    Comment

    Working...