JSFiddle - React, Tailwind, and code Playground
by godhong
HTML
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.0/jquery.validate.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.0/additional-methods.js"></script>
<form id="myform">
<input type="text" name="mm1" value="MM/DD" />
<br/>
<input type="submit" />
</form>
JavaScript
$(document).ready(function () {
$('#myform').validate({ // initialize the plugin
submitHandler: function (form) { // for demo
alert('valid form submitted'); // for demo
return false; // for demo
},
rules: {
mm1: {
dateFormat:true
}
}
});
$.validator.addMethod(
"dateFormat",
function (value, element) {
return isDate(value)
},
"Please enter a date in the format MM/DD");
function isDate(txtDate) {
var currVal = txtDate;
if (currVal === '') return false;
//Declare Regex
var rxDatePattern = /^(\d{1,2})(\/|-)(\d{1,2})$/;
var dtArray = currVal.match(rxDatePattern); // is format OK?
if (dtArray === null) return false;
//Checks for dd/mm format.
var dtDay = dtArray[3];
var dtMonth = dtArray[1];
if (dtMonth.length != 2 || dtMonth < 1 || dtMonth > 12) return false;
else if (dtDay.length != 2 || dtDay < 1 || dtDay > 31) return false;
else if (dtMonth = 2 && dtDay > 29) return false;
else if (dtMonth = 4 || dtMonth = 6 || dtMonth = 9 || dtMonth = 11 && dtDay > 30) return false;
return true;
}
});