开发者

javascript validation need help to add radio support

开发者 https://www.devze.com 2023-03-04 11:56 出处:网络
I am using a script called osDate and I am trying to modify the sign up form to suit my setup. So far I have the below javascript code, but I would like to check a set of radio buttons, but not sure,

I am using a script called osDate and I am trying to modify the sign up form to suit my setup.

So far I have the below javascript code, but I would like to check a set of radio buttons, but not sure, how to add it into the current code.

I am looking for it to check to see if, it has been selected, as by default either are selected, so I am wanting the user to pick one.

When the form is submitted it has the following

<form name="frmSignup" id="frmSignup" method="post" enctype="multipart/form-data" action="register_save.php" onsubmit="javascript:return validateme(this);">

So it really needs to be a part of the current javascript error checking, as onsubmit it calls the javascript code.

Radio button code is:

<input type="radio" value="orange" name="fruit" /> Orange
<input type="radio" value="apple" name="fruit" /> Apple

This is the JavaScript error checking:

var alphanumeric_chars = "0123456789.+-_#,/ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ()_";
var alphanum_chars = "0123456789_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
var text_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz /'";
var full_chars = "0123456789.+-_#,/ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz() _$+=;:?'";


<script type="text/javascript">
/* <![CDATA[ */
function validateme(form)
{
    var tos_ok = form.accept_tos.checked;

    ErrorCount=0;
    ErrorMsg = new Array();
    /* log details */
    CheckFieldString("noblank",form.username,"Please enter the username.");
    CheckFieldString("alphanum",form.username,"Only letters, numbers and underscores '_' are allowed in the username.");
    CheckFieldString("noblank",form.email,"Email must be specified.");
    CheckFieldString("email",form.email,"Email address is not valid.");
    CheckFieldString("noblank",form.firstname,"First Name must be specified.");
    CheckFieldString("text",form.firstname,"Only letters are allowed in First Name.");
    CheckFieldString("noblank",form.lastname,"Last Name must be specified.");
    CheckFie开发者_运维知识库ldString("text",form.lastname,"Only letters are allowed in Last Name.");
    CheckFieldString("noblank",form.address1,"Last Name must be specified.");
    CheckFieldString("full",form.address1,"Only letters, numbers and underscores '_' are allowed in the password.");
    CheckFieldString("full",form.address2,"Only letters, numbers and underscores '_' are allowed in the password.");
    CheckFieldString("noblank",form.city,"Last Name must be specified.");
    CheckFieldString("text",form.city,"Only letters are allowed in Last Name.");

    if(form.username.value.length >= 5 && form.username.value.length <= 20){
        if ( !isNaN(form.username.value.charAt(0)) ){
            ErrorCount++;
            ErrorMsg[ErrorCount] = "Username must start with a letter." ;
        }
    }else{
        ErrorCount++;
        ErrorMsg[ErrorCount] = "Number of characters in username should be between the specified range." ;
    }

    if (tos_ok != true) {
        ErrorCount++;
        ErrorMsg[ErrorCount]="Please read and accept the Terms of Service before registering";
    }       

    /* concat all error messages into one string */
    result="";
    if( ErrorCount > 0)
    {
        result = "---- Following errors occured -----"+ String.fromCharCode(13)+ String.fromCharCode(10);
        for( c in ErrorMsg)
            result += ErrorMsg[c]+ String.fromCharCode(13)+ String.fromCharCode(10)+ String.fromCharCode(10);
        alert(result);
        return false;
    }
    return true;
}
/* ]]> */
</script>


How about this

Within your script tags but outside of your current validateme() function add the below method.

function CheckRadioButtons()
{
    var arr = document.getElementsByName("fruit");
    var pass = false;
    for (chk in arr)
    {
        if(chk.checked)
        {
            pass = true;
        }
    }
    if(!pass)
    {
        return 1;
    }
    return 0;
}

Now add the following lines within validateme() after all your string validation.

if(CheckRadioButton() == 1)
{
    ErrorCount++;
    ErrorMsg[ErrorCount] = "At least one radio button must be selected.";
}

Hope this helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消