Javascript : Validate As You Type


/ Published in: JavaScript
Save to your folder(s)

Javascript : Validate As You Type


Copy this code and paste it in your HTML
  1. //Javascript//
  2.  
  3. function validateAsYouType(inputElementId)
  4. {
  5. var val=inputElementId.value;
  6. var lastLetter=val.substring((val.length-1),val.length);
  7.  
  8. if(isNaN(lastLetter))
  9. {
  10. //Input is OK. Let things be as they are.
  11. }
  12.  
  13. else
  14. {
  15. //Input is not OK. Rmove the last entered character
  16.  
  17. var new_val=val.substr(0,val.length-1);
  18. document.getElementById('inputtype').value=new_val;
  19.  
  20. }
  21.  
  22. }
  23.  
  24. //Javascript//
  25.  
  26.  
  27. Sample usage:
  28.  
  29. <input type="text" value="" id="inputtype" onkeyup="return validateAsYouType(this);" />

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.