Last active
May 14, 2019 07:57
-
-
Save akshayvinchurkar/f862b5db153a103aa624c5a992bc8612 to your computer and use it in GitHub Desktop.
javascript input validations (client side)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// function to get only numbers in input field | |
function validateKeyStrokes(event) { | |
var charCode = (event.which) ? event.which : event.keyCode; | |
if (charCode > 31 && (charCode < 48 || charCode > 57)) { | |
return false; | |
} | |
return true; | |
} | |
// use like this onkeypress="return validateKeyStrokes(event)" | |
// function to get only text in input field | |
function onlyAlphabets(e, t) { | |
try { | |
if (window.event) { | |
var charCode = window.event.keyCode; | |
} | |
else if (e) { | |
var charCode = e.which; | |
} | |
else { return true; } | |
if ((charCode > 64 && charCode < 91) || (charCode > 96 && charCode < 123)) | |
return true; | |
else | |
return false; | |
} | |
catch (err) { | |
alert(err.Description); | |
} | |
} | |
// use like this onkeypress="return onlyAlphabets(event, this)" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment