html表单的javascript检查函数


javascript check function for a html form

我的HTML表单(名称、电子邮件、主题、消息)有问题,我无法验证表单是否填写正确。我想在提交表单时调用一个javascript函数,并在字段填写不正确时发出警报。

我的问题是提交按钮,因为我的老板说我必须使用他自己的php函数来插入提交按钮。php函数看起来像:

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

以及我如何调用函数:

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

我试图调用在<form action='email.php' method='post'> HTML表单中声明onSubmit="return checkForm(this)"的js函数,但我无法"捕获"提交。我还试图用jquery $(document).ready(function() { $('.normal_button').click(function() { }); );来捕捉它,但如果一个字段没有完全正确,我就不能返回false。

我必须使用他的PHP函数来插入提交按钮,但我如何才能"捕获"字段,如何验证它们?有什么建议吗?非常感谢。

首先,为该表单和sbmitdiv按钮设置ID。

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

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

然后使用jQuery:进行检查

$('#mailFormSubmit').click(function(){
    // fields check code goes here
....
    // if all fields are OK, then submit form
    $('#mailForm').submit();
});