我正在使用一个名为osDate的脚本,我正在尝试修改注册表单以适应我的设置。
到目前为止,我有以下javascript代码,但我想查看一组单选按钮,但不确定如何将其添加到当前代码中。
我正在寻找它以检查它是否已被选中,因为默认情况下已被选中,所以我希望用户选择一个。
提交表单时,它具有以下内容
<form name="frmSignup" id="frmSignup" method="post" enctype="multipart/form-data" action="register_save.php" onsubmit="javascript:return validateme(this);">
所以它确实需要成为当前javascript错误检查的一部分,因为onsubmit它调用了javascript代码。
单选按钮代码为:
<input type="radio" value="orange" name="fruit" /> Orange
<input type="radio" value="apple" name="fruit" /> Apple
这是JavaScript错误检查:
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.");
CheckFieldString("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>
答案 0 :(得分:1)
这个怎么样
在您的脚本标记内,但在当前的validateme()函数之外添加以下方法。
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;
}
现在,在所有字符串验证之后,在validateme()中添加以下行。
if(CheckRadioButton() == 1)
{
ErrorCount++;
ErrorMsg[ErrorCount] = "At least one radio button must be selected.";
}
希望这有帮助。