jQuery:如何验证两个字段中的一个

时间:2015-06-17 16:07:37

标签: jquery forms validation

在我的表格中有两个字段,其id为“partita_iva”和“codice_fiscale”。我需要它们中的至少一个不是空的。所以,看看stackoverflow我发现了以下内容:

$linkone = 'https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=25&playlistId={playlistID}&key={Key}';
$linktwo = 'https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=25&playlistId={PlaylistID}&key={Key}';

$jsonArray = array(
  json_decode(file_get_contents($linkone)),
  json_decode(file_get_contents($linktwo))
);

foreach($jsonArray as $json) {
  foreach($json['items'] as $row)
  {
    $title = $row['snippet']['title'];
    $title = mysql_real_escape_string($title);
    $description = $row['snippet']['description'];
    $description = mysql_real_escape_string($description);

    $publishedAt = $row['snippet']['publishedAt'];
    $publishedAt = mysql_real_escape_string($publishedAt);
    $high = $row['snippet']['thumbnails']['high']['url'];
    $high = mysql_real_escape_string($high);

    $sql = "INSERT INTO table(title, description, publishedAt, high)
      VALUES('".$title."','".$description."','".$publishedAt."','".$high."')";

    if(!mysql_query($sql,$conn))
    {
      die('Error : ' . mysql_error());
    }
  }
}

消息是

codice_fiscale: {
        required: "#partita_iva:empty",
        remote: {
            url: "include/utils/checkCliente.php",
            type: "post",
            data: {
                id: id_cliente,
                cf_ck: function() {
                    return jQuery("#codice_fiscale").val();
                }
            }
        }
    },

    partita_iva: {
        required: "#codice_fiscale:empty",
        remote: {
            url: "include/utils/checkCliente.php",
            type: "post",
            data: {
                id: id_cliente,
                piva_ck: function() {
                    return jQuery("#partita_iva").val();
                }
            }
        }
    },

因此,如果两者都是空的,我会正确地得到两者的错误消息。不幸的是,如果我填写“codice_fiscale: { remote: "Codice fiscale già esistente!", required: "Inserisici il codice fiscale del cliente privato", }, partita_iva: { required: "Inserisci la partita IVA del cliente", remote: "Partita IVA già esistente!", }, ”或“codice_fiscale”,那么我仍会收到其他字段的错误消息...我在哪里犯了错误?

SOLUTION:

partita_iva

注意:符合/ *** /使用

<script type="text/javascript" src="js/plugins/jquery.validate.min.js"></script>

codice_fiscale:{

 //required: true, 
 required: function(element) {
             return !jQuery("#partita_iva").is(':filled'); /***/
           },
...
}

partita_iva:{

 //required: true, 
 required: function(element) {
             return !jQuery("#codice_fiscale").is(':filled'); /***/
           },
...
}

因为

:filled 

(正如我之前所做的那样)不起作用,并且可以使用它。

0 个答案:

没有答案