简体   繁体   English

Javascript表单验证单选和复选框

[英]Javascript form validation radio and check boxes

OK I have a new problem with my JS form validation project. 好的,我的JS表单验证项目有一个新问题。

I can't get the radio and checkboxes to validate and throw up error messages. 我无法通过广播和复选框来验证并抛出错误消息。

I have a external .js script with function rules linked in my html within the head section. 我有一个外部.js脚本,其中的功能规则链接在我的html头部分中。

The function rules look like this: 函数规则如下所示:

    /* ======================================================================
FUNCTION:   IsValid4DigitZip

INPUT:      str (string) - a 5-digit zip code to be tested

RETURN:     true, if the string is 5-digits long
        false, otherwise

CALLS:  IsBlank(), IsInt() which are defined elsewhere in the Script Library

PLATFORMS:  Netscape Navigator 3.01 and higher,
            Microsoft Internet Explorer 3.02 and higher,
            Netscape Enterprise Server 3.0,
            Microsoft IIS/ASP 3.0.
====================================================================== */
function IsValid4DigitZip( str ) {
    // Return immediately if an invalid value was passed in
    if (str+"" == "undefined" || str+"" == "null" || str+"" == "")  
        return false;

    var isValid = true;

    str += "";

    // Rules: zipstr must be 5 characters long, and can only contain numbers from
   // 0 through 9
   if (IsBlank(str) || (str.length != 4) || !IsInt(str, false))
        isValid = false;

   return isValid;
} // end IsValid4DigitZip

How would I add function rules to my external .js script to validate the radio and checkbox forms in my html. 如何在我的外部.js脚本中添加函数规则,以验证HTML中的单选和复选框形式。 Can someone guide me on how to write the function rules? 有人可以指导我如何编写函数规则吗?

And how would I call these rules for radio and checkboxes in the section? 在本节中,我将如何称呼这些单选和复选框规则?

validation for checkbox and Radio button is Checked function checkboxRadio按钮的验证为“已Checked function

In javascript 在JavaScript中

<script>
if (document.getElementById("male").checked || document. 
getElementById("female").checked)
{
  //messages
}
</script>

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

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