I already have this regular expression:
function check(){
var re = /[0-9A-Fa-f]{6}/g;
var inputString = document.getElementById("check").value;
if(re.test(inputString)) {
window.alert("Valid hex colour")
} else {
window.alert("Invalid hex colour")
}
re.lastIndex = 0;
}
And this is the input:
<input id="check" maxlength="6" placeholder="------">
This is the event listener:
document.getElementById('check').onkeydown=function(cc){
if(cc.keyCode==13){
check()
}
else if(cc.keyCode==46){
document.getElementById("check").value = "";
}
}
This works fine. I also tried this regular expression:
/[0-9A-Fa-f]{3}|[0-9A-Fa-f]{6}/g;
But this also accepts hex values that are 3, 4, 5 or 6 characters long. How can you check for hex values that are exactly 3 or exactly 6 characters long using a regular expression? Thanks in advance.