By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,692 Members | 1,693 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,692 IT Pros & Developers. It's quick & easy.

Username should take alphabets

P: 7
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::

Expand|Select|Wrap|Line Numbers
  1. <tr>
  2.        <td style="width:30%" align="right">User Name :</td>
  3.        <td style="width:70%"> <input name="username" size="20" maxlength="155" value="<?php echo $username;?>"   onfocus="setVisibility('100','inline')"; onBlur="setVisibility('100','none')"> </td>
  4.      </tr>
  5. <tr>
  6.        <td align="right">Password :</td>
  7.        <td> <input name="password" type="password" size="20" maxlength="155" value="<?php echo $password;?>"   onfocus="setVisibility('100','inline')"; onBlur="setVisibility('100','none')"> </td>
  8.  
  9.        </tr>
  10.  
  11.  
  12. <script type="text/javascript">
  13.  
  14.  
  15. function validate_form ( )
  16. {
  17.     valid = true;
  18.  
  19.     if ( document.msgform.username.value == "" )
  20.     {
  21.         alert ( "Please fill the UserName Details." );
  22.         valid = false;
  23.     }
  24.     else if(document.msgform.password.value == "")
  25.     {
  26.      alert ( "Please fill the Password Details." );
  27.         valid = false;
  28.     }
  29. return valid;
  30.  
  31. }    
  32.  
  33. <td>
  34.      <td>  <input type="submit" name="submit" value="Submit"  onClick="return validate_form();"></td></div>
  35.        </td>
  36.  
  37.  
  38. </script>
  39.  
  40.  
Please can anybody tellme how to write validations for this username that it should accept only Alphabets.

Regards,
meeanji
Jun 24 '08 #1
Share this Question
Share on Google+
1 Reply


acoder
Expert Mod 15k+
P: 16,027
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.
Expand|Select|Wrap|Line Numbers
  1. var username = document.msgform.username.value;
  2. if (!username.match(/^[a-z]+$/i) {
  3.   // you can alert here...
  4.  
Jun 24 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.