Aaron F
Aaron F

Reputation: 47

html javascript php zip code data validation

I am adding a data validation to my code. I want to make sure the event title is not blank, and the zip code entered is valid. The javascript does not work. Can you tell me why?

<form name="form1" action="organize.php" onSubmit="return validateform1();" method="post" enctype="multipart/form-data">            

    <div class="form-element">
        <label for="event_title">Event Title</label>
        <input type="text" name="event_title" id="dob" />
    </div>

    <div class="form-element">
        <label for="zip">Zip Code</label>
        <input type="text" name="zip" id="dob" />
    </div>

    <script language="javascript">

        function validateform1(){        

            var zipcode = /^\+?([0-9]{2})\)?[-. ]?([0-9]{4})[-. ]?([0-9]{4})$/;        
            if(document.form1.event_title.value==""){
                alert("Enter a title");
                return false;
            }        
            else if((!document.form1.zip.value.match(zipcode)){  
                alert("wrong zip format");  
                return false;  
            }  
    </script>

</form>

Upvotes: 0

Views: 1492

Answers (2)

Awlad Liton
Awlad Liton

Reputation: 9351

you have missed closing ) in this line:

else  if((!document.form1.zip.value.match(zipcode))  { 

should be :

else  if((!document.form1.zip.value.match(zipcode)))  { 

or:

else  if(!document.form1.zip.value.match(zipcode))  { 

Always you should see browser console.you can see hint to get rid of your issues.

Upvotes: 2

nnnnnn
nnnnnn

Reputation: 150030

Probably doesn't help that your ZIP field has id="dob", but I think the main issue is that your function is missing a closing }, and your else if (( has an extra (. Try this:

function validateform1() {

    var zipcode = /^\+?([0-9]{2})\)?[-. ]?([0-9]{4})[-. ]?([0-9]{4})$/;
    if (document.form1.event_title.value == "") {
        alert("Enter a title");
        return false;
    } else if (!document.form1.zip.value.match(zipcode)) {
        alert("wrong zip format");
        return false;
    }
}

Upvotes: 1

Related Questions