0

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.

2
  • preg_match() is for PHP. This is JavaScript. You need to use JavaScript regex. Commented Oct 22, 2013 at 5:18
  • oh.... @Pietu1998 Thanks... I am from asp.net and was confused on how the php is working .... Thanks... wasted lot of time here... Commented Oct 22, 2013 at 5:23

1 Answer 1

1

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)) { ... }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.