This entry was posted on Aug 09 2007
Quick Regex form validation to check if a field contains numbers only...
JavaScript:
-
function isNumeric(elem, msg){
-
var alphaExp = /^<strong>[0-9]</strong>+$/;
-
if(elem.value.match(alphaExp)){
-
return true;
-
}else{
-
alert(msg);
-
elem.focus();
-
return false;
-
}
-
}
now letters only...
JavaScript:
-
function isAlpha(elem, msg){
-
var alphaExp = /^<strong>[a-zA-Z]</strong>+$/;
-
if(elem.value.match(alphaExp)){
-
return true;
-
}else{
-
alert(msg);
-
elem.focus();
-
return false;
-
}
-
}
now alphaNumeric...
JavaScript:
-
function isAlphaNumeric(elem, msg){
-
var alphaExp = /^<strong>[0-9a-zA-Z]</strong>+$/;
-
if(elem.value.match(alphaExp)){
-
return true;
-
}else{
-
alert(msg);
-
elem.focus();
-
return false;
-
}
-
}
now alphaNumeric with underscores...
JavaScript:
-
function isNumeric(elem, msg){
-
var alphaExp = /^<strong>[0-9a-zA-Z_]</strong>+$/;
-
if(elem.value.match(alphaExp)){
-
return true;
-
}else{
-
alert(msg);
-
elem.focus();
-
return false;
-
}
-
}
now with a highlight on the field in question...
JavaScript:
-
function isAlphaNumeric(elem, msg){
-
var alphaExp = /^[0-9a-zA-Z_]+$/;
-
if(elem.value.match(alphaExp)){
-
return true;
-
}else{
-
alert(msg);
-
<strong>elem.style.backgroundColor='#ff0000';</strong>
-
elem.focus();
-
return false;
-
}
-
}
usage (place the following in your opening form tag):
JavaScript:
-
onsubmit="return isAlphaNumeric(document.getElementById('the_field_in_question'), 'your message!')"
... and make sure the input field itself has an ID of 'the_field_in_question'
Enjoy.
Meshach
hi, I found your cloud search script on iloveyoutube.com and I'm trying to implement it on my web site, but without sucsess. I have different database, but I think that shouldn't be a problem but I always get an mysql_fetch_array(): supplied argument is not a valid MySQL result resource error... can you help?