I am currently using html to code a 'form entry'. I am also using a JavaScript validation, to validate the input in of the form. At the moment, i have 'name', 'subject' and 'examination number', each working and having functional validations. However i need to add a validation for 'qualification'. The type of input has to be click select and the 'qualification' has to be radio buttons. there should be three radio buttons called 'GCSE', 'AS' and 'A2'. it would be great if someone could help me with the radio buttons, and the user should only be able to click and select one type of qualification at one time. after clicking the qualification, the user needs to be informed by a message that they have chosen their qualification 'you have selected GCSE as your qualification' this message should be immediately after they clicked their qualification. GCSE is just an example, it could be AS or A2 or GCSE. thanks.

here is my code: the radio buttons are near the bottom, but the validaiton should be below the validation of 'examination number'

<head>

<title>Exam Entry</title>

<script language="javascript" type="text/javascript">

function validateForm(e) {

var result = true;
var msg="";

if (document.ExamEntry.name.value=="") {
    msg+="You must enter your name \n";
    document.ExamEntry.name.focus();
    document.getElementById('name').style.color="red";
    result = false;
}

if (document.ExamEntry.subject.value=="") {
    msg+="You must enter the subject \n";
    document.ExamEntry.subject.focus();
    document.getElementById('subject').style.color="red";
    result = false;
}

var regex = /^\d{4}$/;
if (document.ExamEntry.Examination_number.value == "") {
msg+="You must enter your examination number";
result = false;
} else if (isNaN(document.ExamEntry.Examination_number.value)) {
msg+="Examination number should only contain digits";
result = false;
} else if (!regex.test(document.ExamEntry.Examination_number.value)) {
msg+="Examination number should contain exactly 4 digits";
result = false;
}

if (msg != "") {
    alert(msg);
}

return result;
}
</script>



</head>

<body>
<h1>Exam Entry Form</h1>
<form name="ExamEntry" method="post" action="success.html" onsubmit="return    validateForm();">
<table width="60%" border="0">
    <tr>
        <td id="name">Name</td>
        <td><input type="text" name="name" /></td>
    </tr>
    <tr>
        <td id="subject">Subject</td>
        <td><input type="text" name="subject" /></td>
    </tr>
        <td id="Examination_number">Examination number</td>
        <td><input type="text" maxlength="4" name="Examination_number" /></td>
    </tr>
    <tr>
<td id="qualification">Choose your qualification</td>
<tr>
<td>
<input type="radio" name="group1" value="GCSE">GCSE<br>
</td>
</tr>
<tr>
<td>
<input type="radio" name="group1" value="AS">AS<br>
</td>
</tr>
<tr>
<td>
<input type="radio" name="group1" value="A2">A2<br>
</td>
</tr>
<tr>
    <td><input type="submit" name="Submit" value="Submit" /></td>
    <td><input type="reset" name="Reset" value="Reset" /></td>
    </tr>
</table>
</form>
</body>

</html>   
有帮助吗?

解决方案

Here is how to add radio validation to your code. (Note this is setup for name="group1" radios only, you will need a for loop to check group2, group3, etc.)

if (document.ExamEntry.group1.value=="") {
    msg+="You must choose a qualification\n";
    document.ExamEntry.subject.focus();
    document.getElementById('radioqual').style.color="red";
    result = false;
}

You notify the user of what qualification they clicked by onclick events on each radio.

The function

function qualinform(qualname) {
  alert(qualname + " was selected as your qualification.");
}

Example of a radio button (The name should be put into the onclick parameters)

<input onclick="qualinform('GCSE');" type="radio" name="group1" value="GCSE">GCSE

Here is the complete code.

<html>
<head>

<title>Exam Entry</title>

<script language="javascript" type="text/javascript">
function qualinform(qualname) {
  alert(qualname + " was selected as your qualification.");
}

function validateForm(e) {

var result = true;
var msg="";

if (document.ExamEntry.name.value=="") {
    msg+="You must enter your name \n";
    document.ExamEntry.name.focus();
    document.getElementById('name').style.color="red";
    result = false;
}

if (document.ExamEntry.subject.value=="") {
    msg+="You must enter the subject \n";
    document.ExamEntry.subject.focus();
    document.getElementById('subject').style.color="red";
    result = false;
}

if (document.ExamEntry.group1.value=="") {
    msg+="You must choose a qualification\n";
    document.ExamEntry.subject.focus();
    document.getElementById('radioqual').style.color="red";
    result = false;
}

var regex = /^\d{4}$/;
if (document.ExamEntry.Examination_number.value == "") {
msg+="You must enter your examination number";
result = false;
} else if (isNaN(document.ExamEntry.Examination_number.value)) {
msg+="Examination number should only contain digits";
result = false;
} else if (!regex.test(document.ExamEntry.Examination_number.value)) {
msg+="Examination number should contain exactly 4 digits";
result = false;
}

if (msg != "") {
    alert(msg);
}

return result;
}
</script>



</head>

<body>
<h1>Exam Entry Form</h1>
<form name="ExamEntry" method="post" action="success.html" onsubmit="return     validateForm();">
<table width="60%" border="0">
    <tr>
        <td id="name">Name</td>
        <td><input type="text" name="name" /></td>
    </tr>
    <tr>
        <td id="subject">Subject</td>
        <td><input type="text" name="subject" /></td>
    </tr>
        <td id="Examination_number">Examination number</td>
        <td><input type="text" maxlength="4" name="Examination_number" /></td>
    </tr>
    <tr>
<td id="qualification">Choose your qualification</td>
<tr>
<td id="radioqual">
<input onclick="qualinform('GCSE');" type="radio" name="group1" value="GCSE">GCSE<br>
<input onclick="qualinform('AS');" type="radio" name="group1" value="AS">AS<br>
<input onclick="qualinform('A2');" type="radio" name="group1" value="A2">A2<br>
</td>
</tr>
    <tr>
        <td><input type="submit" name="Submit" value="Submit" /></td>
        <td><input type="reset" name="Reset" value="Reset" /></td>
    </tr>
</table>
</form>
</body>

</html>

其他提示

Loop over each of the form elements named 'qualification', and when you find the checked one, grab its value and push it into your validation message like this:

var qualifications = document.getElementsByName('group1') 

for (var i = 0, length = qualifications.length; i < length; i++) {
    if (radios[i].checked) {
        msg+='you have selected ' + radios[i].value + ' as your qualification.';
        break;
    }
}

Okay I have a solution for you :) Try this

Then allocate ids to your inputs as a1, a2 & a3

<script language="javascript" type="text/javascript">

function validateForm(e) {

var result = true;
var msg="";

if (document.ExamEntry.name.value=="") {
msg+="You must enter your name \n";
document.ExamEntry.name.focus();
document.getElementById('name').style.color="red";
result = false;
}

if (document.ExamEntry.subject.value=="") {
msg+="You must enter the subject \n";
document.ExamEntry.subject.focus();
document.getElementById('subject').style.color="red";
result = false;
}

var regex = /^\d{4}$/;
if (document.ExamEntry.Examination_number.value == "") {
msg+="You must enter your examination number";
result = false;
} else if (isNaN(document.ExamEntry.Examination_number.value)) {
msg+="Examination number should only contain digits";
result = false;
} else if (!regex.test(document.ExamEntry.Examination_number.value)) {
msg+="Examination number should contain exactly 4 digits";
result = false;
}

if(document.getElementById('a1').checked){
    if (msg != "") {
    alert(msg);
    }

    return result;  
}
else{
    if(document.getElementById('a2').checked){
        if (msg != "") {
        alert(msg);
        }

        return result;   
    }
    else{
        if(document.getElementById('a3').checked){
             if (msg != "") {
             alert(msg);
             }

             return result;   
        }
        else{
            msg+="Please select an option";
            result = false;
            if (msg != "") {
            alert(msg);
            }

            return result;
        }   
    }      
}


}
</script>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top