calling a java script form validation

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cali_dotcom
Forum Commoner
Posts: 49
Joined: Fri Aug 22, 2008 7:28 pm
Location: Rancho Cucamonga, CA

calling a java script form validation

Post by cali_dotcom »

can anyone tell me why this does not work:

Code: Select all

 
    <div>
    <table>
    <FORM name="search_form"  method='POST' onSubmit="return validate_form();" ACTION='jobsearch.php'>
    <tr><td>keyword:</td><td >
        <INPUT TYPE='TEXT' NAME='keyword'></td>
        <td>Industry:</td><td >
        <select name="cat_id">
        <option value="">---select one---</option>
        <option value="1">Accounting</option>
        <option value="2">Administration</option>
        <option value="3">Automation</option>
        <option value="4">Banking</option>
        <option value="5">Biotech</option>
        <option value="6">Bussiness</option>
        <option value="7">Construction</option>
        <option value="8">Construction</option>
        <option value="9">Consulting</option>
        </select></td></tr>
        <tr><td>city and state or zipcode: </td><td >
        <INPUT TYPE='TEXT' NAME='place'></td></tr>
        
        <tr ><td colspan="2"><input type="submit" value="Search"></td></tr>
    </form>
    </table>
    </div>
 
the javascript containing the function validate_form was linked in the header section:

Code: Select all

 
<HEAD>
<link href='style_sbdjobs.css' type='text/css' rel='stylesheet' />
 
<script src="reg_form.js"></script>
 
 
 
 
</HEAD>
 
the problem is when the submit button is clicked, the javascript function is not called instead the php function is called directly but i need the form to be validated first before it is sent to the server. here is what the javascript reg_form.js looks like:

Code: Select all

 
function validate_form( )
{
    valid = true;
 
        if ( document.search_form.keyword.value == "" )
        {
                alert ( "Please type in a keyword." );
                valid = false;
                break;
        }
 
        
 
        if ( document.search_form.cat_id.selectedIndex == 0 )
        {
                alert ( "Please select an industry." );
                valid = false;
                break;
        }
        
        if ( document.search_form.place.value == "" )
        {
                alert ( "Please type in a zipcode or city and state." );
                valid = false;
                break;
        }
        
 
 
        return valid;
}
 
 
koen.h
Forum Contributor
Posts: 268
Joined: Sat May 03, 2008 8:43 am

Re: calling a java script form validation

Post by koen.h »

Hi,

I'm far from comfortable with javascript but some suggestions:

1) don't use the name attribute in form elements. They are deprecated. Try to use an id and getElementById.
2) I prefer to have as little javascript inside my html as possible. You can eg do something like this:

Code: Select all

 
window.onload=function () { 
 if (document.getElementById) { 
  document.getElementById('test-form').onsubmit = function() { 
 return validate(document.getElementById('test-form')); 
} 
 }
see: http://www.webmasterworld.com/javascript/3701456.htm
cali_dotcom
Forum Commoner
Posts: 49
Joined: Fri Aug 22, 2008 7:28 pm
Location: Rancho Cucamonga, CA

Re: calling a java script form validation

Post by cali_dotcom »

[quote="koen.h"]

You can eg do something like this:

Code: Select all

 
window.onload=function () { 
 if (document.getElementById) { 
  document.getElementById('test-form').onsubmit = function() { 
 return validate(document.getElementById('test-form')); 
} 
 }

i'm also not too comfortable with javascript. where does this code go?
koen.h
Forum Contributor
Posts: 268
Joined: Sat May 03, 2008 8:43 am

Re: calling a java script form validation

Post by koen.h »

Put it at the end of reg_form.js.
Post Reply