Last active
May 18, 2017 04:53
-
-
Save siddharthkrish/b3ffa5d4e34136516a5e23f7625025e9 to your computer and use it in GitHub Desktop.
form input field that only accepts numbers
This file contains hidden or 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> | |
<script type="text/javascript"> | |
$( document ).ready( function () { | |
console.log("ready"); | |
// check to see if an object with class .date has lost focussed. if so check | |
// the value of the input for the date type dd/mm/yyyy (only 1900-2099) | |
$(".date").blur( function (e) { | |
var dateRegex = /^(0[1-9]|1\d|2\d|3[01])\/(0[1-9]|1[0-2])\/(19|20)\d{2}$/ ; | |
var fieldValue = $(e.target).val(); | |
if (dateRegex.test(fieldValue)) { | |
// validation passed | |
console.log("good value"); | |
} else { | |
// validation failed | |
console.log("bad value"); | |
} | |
}); | |
}); | |
</script> | |
</head> | |
<body> | |
<form> | |
<input type="text" name="age" class="date" placeholder="dd/mm/yyyy"> | |
</form> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment