Reteras Remus
Reteras Remus

Reputation: 933

javascript check function for a html form

I'm having a problem with a HTML form (name, e-mail, subject, message), I can't validate the form if it was completed correct or not. I want to call a javascript function when the form is submitted and alert if a field wasn't completed correct.

My problem is with the submit button, because my boss said that I must use he's own php function to insert a submit button. The php function looke like:

function normal_button($text, $width, $param = '', $class = 'button-blue')
{
    $content = '<div class="'.$class.'" style="width:'.$width.'px;" '.$param.'>'.$text.'</div>';

    return $content;
}

and how I call the function:

echo normal_button('Send it', '100', 'onclick="document.getElementById(\'emailForm\').submit();" ');

I tried to call the js function declaring the onSubmit="return checkForm(this)" in the <form action='email.php' method='post'> HTML form, but I can't "catch" the submit. Also I tried to catch it with jquery, $(document).ready(function() { $('.normal_button').click(function() { }); ); but I can't return false if a field wasn't completed correct.

I must use he's PHP function to insert the submit button, but how can I "catch" the fields, how can I validate them? Any suggestions? Thank you !

Upvotes: 0

Views: 281

Answers (1)

urosevic
urosevic

Reputation: 579

First of all, set ID for that form and for sbmit DIV button.

<form action='email.php' method='post' id='mailForm'>

and

echo normal_button('Send it', '100', 'id="mailFormSubmit"');

then do check with jQuery:

$('#mailFormSubmit').click(function(){
    // fields check code goes here

....

    // if all fields are OK, then submit form
    $('#mailForm').submit();
});

Upvotes: 1

Related Questions