Validating email
<input pattern="/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/" required />
I need to make sure one part of the form only accepts alpha numeric characters
<!DOCTYPE html>
<html>
<head>
<title>Validate</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<style>
.widefat-main{
}
</style>
<script>
function validate() {
var errorDiv = document.getElementById("errorDiv"),
regex = /^[a-z0-9]+$/,
str = document.getElementById("inputString").value;
if ((str.length > 4) && (str.length < 10) && regex.test(str)) {
errorDiv.innerHTML = "Fine string";
return true;
}
else {
errorDiv.innerHTML = "4 to 10 alphanumerical characters only";
return false;
}
}
</script>
</head>
<body>
<form action="" onsubmit="return validate();">
<input
id="inputString"
type="text"
name="url_code"
class="widefat-main"
title="4 to 10 alphanumerical characters only"
/>
<input type="submit" value="Submit"/>
</form>
<div id="errorDiv"></div>
</body>
</html>
I need to make sure at least one box has been ticked
var f = document.form1;
if (f.summer.checked || f.autumn.checked || f.winter.checked) {
// we're OK!
}
and as I said in the title, make sure the expiry date box only accepts a "/" or numbers.
var errormessage = "";
function checkIsValidDateTime(){
errormessage = "";
var isvalid = false;
var dt = document.getElementsByName("date")[0].value;
var parts = dt.split(" ");
if(parts.length == 3){
var date = parts[0];
var time = parts[1];
var ampm = parts[2];
if(ampm.length == 2 && (ampm.toLowerCase() == "am" || ampm.toLowerCase() == "pm")){
var validformatdate=/^\d{2}\/\d{2}\/\d{4}$/;
if (validformatdate.test(date)){
var validformattime = /^\d{1,2}:\d{2}$/;
if (validformattime.test(date)){
isvalid = true;
}else{errormessage = "Time is not in the format HH:MM";}
}else{errormessage = "Date is not in the format dd/mm/yyyy";}
}else{errormessage = "DateTime does not have AM/PM at the end";}
}else{errormessage = "DateTime is not in the format 11/07/2011 3:45 AM";}
return isvalid;
}