简体   繁体   中英

Javascript form validation radio and check boxes

OK I have a new problem with my JS form validation project.

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.

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. 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

In javascript

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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