Posted By

welancers on 08/15/12


Tagged

javascript username validate


Versions (?)

Validate Username using Javascript


 / Published in: JavaScript
 

Validate Username using Javascript. Pass the Username as argument to function. It allows to enter only letters, numbers and underscores.

  1. function validateUsername(str) {
  2. var error = "";
  3. var illegalChars = /\W/; // allow letters, numbers, and underscores
  4.  
  5. if (str == "") {
  6. error = "&bull; Please enter Username<br>";
  7. } else if ((str.length < 5) || (str.length > 15)) {
  8. error = "&bull; Username must have 5-15 characters<br>";
  9. } else if (illegalChars.test(str)) {
  10. error = "&bull; Please enter valid Username. Use only numbers and alphabets<br>";
  11. } else {
  12. error = "";
  13. }
  14. return error;
  15. }

Report this snippet  

You need to login to post a comment.