Reputation: 25
I am trying to validate the PHP page input with regular expression but I am totally new here and need some assistance
<script>
function addplaces()
{
valid=true;
placename=document.getElementById("placename").value;
city=document.getElementById("city").value;
province=document.getElementById("province").value;
country=document.getElementById("country").value;
category=document.getElementById("category").value;
placepicture=document.getElementById("placepicture").value;
descp=document.getElementById("descp").value;
if(placename=="" || preg_match("^[A-Z]'?[- a-zA-Z]( [a-zA-Z])*$", placename))
{
alert("Please Enter Place Name");
document.getElementById("placename").focus();
valid=false;
}
else if(city=="")
{
alert("Please Enter City Name");
document.getElementById("city").focus();
valid=false;
}
else if(province=="")
{
alert("Please Enter province Name");
document.getElementById("province").focus();
valid=false;
}
else if(country=="")
{
alert("Please Enter country Name");
document.getElementById("country").focus();
valid=false;
}
else if(category=="")
{
alert("Please Enter category Name");
document.getElementById("category").focus();
valid=false;
}
else if(placepicture=="")
{
alert("Please Enter place picture");
document.getElementById("placepicture").focus();
valid=false;
}
else if(descp=="")
{
alert("Please Enter Description");
document.getElementById("descp").focus();
valid=false;
}
return valid;
}
</script>
I try to use preg_match();
but it does not work, please let me know where I am making a mistake.
Upvotes: 0
Views: 1494
Reputation: 16754
preg_match
is for PHP, that's why it doesn't work in your javascript code.
instead of preg_match("^[A-Z]'?[- a-zA-Z]( [a-zA-Z])*$"
(you missed a )
here if you would programming in PHP)
use
var regex = new Regex( /^[A-Z]'?[- a-zA-Z]( [a-zA-Z])*$ );
if(regex.test(placename)) { ... }
Upvotes: 1