简体   繁体   English

表单验证-规则帮助

[英]Form validation - help with rules

Hi I'm writing and app in PHP. 嗨,我正在用PHP编写应用程序。 I want to use jquery form validation but the situation I have are 'from' and 'to' fields. 我想使用jquery表单验证,但是我遇到的情况是“ from”和“ to”字段。 If one of these used and the other is blank I want to stop the form submit. 如果其中一个使用而另一个使用空白,我想停止提交表单。

So user can only submit the form if to and from are either both empty or both contain dates. 因此,仅当to和from都为空或都包含日期时,用户才能提交表单。

Here are the fields: 这些是字段:

<table class="search-form">
<tr>
    <td><label>Client</label></td>
    <td></td>
    <td><?php echo form_dropdown('client', $clients, $current_client);?></td>
</tr>
<tr>
    <td><label>Date From: </label></td>
    <td></td>
    <td><input type="text" name="from_date" class="datepicker" value="<?php echo $from; ?>"/></td>
</tr>
<tr>
    <td><label>Date To: </label></td>
    <td></td>
    <td><input type="text" name="to_date" class="datepicker" value="<?php echo $to; ?>" /></td>
</tr>
<tr>
    <td></td>
    <td></td>
    <td><input type="submit" name="submit" value="Search" /></td>
</tr>

Can someone steer me in the right direction here. 有人可以在这里指引我正确的方向。

Thanks, 谢谢,

Billy 比利

There may be something built into jQuery to handle this, but I can't think of it if there is. jQuery中可能内置了一些东西来处理此问题,但是我想不到。 You could simply attach a function to the submit button that does some quick checking like this: 您可以简单地将一个函数附加到“提交”按钮上,以进行如下快速检查:

function(){ //attached to the $('form').submit
if (($("from_date").text == "" && $("to_date").text == "")
|| $("from_date").text != "" && $("to_date").text != ""))
    return true;
else
    return false; //return false should prevent the form from from being submitted

You need to add form tag and then in jquery do 您需要添加表单标记,然后在jquery中执行

$('form').submit(function() {
//validation here
 if(success)return true;
   else return false;
});

return false means that default submit function is not launched 返回false表示未启动默认的提交功能

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM