我正在用 JavaScript 编写生日验证表单,但它不能正常工作。例如,40/40/2012 是无效的,但它不会发出任何警报。
JavaScript
function onFormSubmit(form_element) {
var birthday = form_element.birthday.value;
if ( !/\d{2}\/\d{2}\/\d{4}/.test(form_element.birthday.value) )
{
alert("This field is required. Please enter date mm/dd/yyyy!");
return false;
}
return true;
}
HTML
<form onsubmit="return onFormSubmit(this)">
Birthday:<input type="text" name="birthday" /><br />
<input type="submit" value="submit" />
</form>