The radio camera does not work in

I have 4 radio buttons in my form, as soon as I submit the form, which any of the radio buttons should check if a warning message does not appear. it works correctly in chrome, firefox, but in that alone I checked the radion, it always shows a warning, so I can not submit the form, I gave my code below, please help me

PHP:

<form action="user_register.php" method="POST" name="myForm" onsubmit="return validateForm()" enctype="multipart/form-data">
    <label>USERNAME:</label></td>
    <input type="text" name="username" class="regtext" required/>
    <label>RESIDING CITY:</label></td>
    <input type="text" name="city" class="regtext" required/>
    <label>I'M A</label>
    <label>ARTIST &nbsp <input type="radio" value="1" name="user_type" >    </label>&nbsp
    <label>MODEL &nbsp <input type="radio" value="2" name="user_type"></label>&nbsp
    <label>COMPOSER &nbsp <input type="radio" value="3" name="user_type" ></label>&nbsp<br>
    <label>BEAT MAKER &nbsp <input type="radio" value="4" name="user_type" ></label>&nbsp
    <label>NONE &nbsp <input type="radio" value="0" name="user_type" ></label>
    <label> <input type="checkbox" value="1" name="letter" > &nbsp  I WOULD LIKE TO RECEIVE YOUR NEWSLETTER</label>
    </div>
    <div class="mainhead">
        <input type="submit" name="register" class="submit" value="SEND AND REGISTER NOW">
    </div>
</form>

JS:

<script type="text/javascript">
    function validateForm() {
        var province = document.forms["myForm"]["province"].value;
        if (province == 0 ) {
            alert("Select Province");
            document.myForm.province.focus() 
            return false;
        }
        var user_type = document.forms["myForm"]["user_type"].value;
        if (user_type == null || user_type == "") {
            alert("Select Who You are");
            return false;
        }
        var letter = document.forms["myForm"]["letter"].value;
        if (letter == null || letter == "") {
            alert("Select that you want to receive news letter");
            return false;
        }
    }
</script>
+4
source share
3 answers

The problem is that for IE it document.forms["myForm"]["user_type"]is an HTMLCollection and does not havevalue

The solution is to change

var user_type = document.forms["myForm"]["user_type"].value;

to

var user_type = document.querySelector('form[name="myForm"] input[name="user_type"]:checked').value;

+1
source

What I noticed is:

province, ( ). , .

<script type="text/javascript">
function validateForm() {
    var province = document.forms["myForm"]["province"].value;
    if (province == 0 ) {
        alert("Select Province");
        document.myForm.province.focus() 
        return false;
    }
    var user_type = document.forms["myForm"]["user_type"].value;
        if (user_type == null || user_type == "") {
        alert("Select Who You are");
        return false;
    }
    var letter = document.forms["myForm"]["letter"].value;
        if (letter == null || letter == "") {
        alert("Select that you want to receive news letter");
        return false;
    }
}
</script>

province. .

<script type="text/javascript">
function validateForm() {

    var user_type = document.forms["myForm"]["user_type"].value;
        if (user_type == null || user_type == "") {
        alert("Select Who You are");
        return false;
    }
    var letter = document.forms["myForm"]["letter"].value;
        if (letter == null || letter == "") {
        alert("Select that you want to receive news letter");
        return false;
    }
}
</script>

, - : " , ". . province validateForm() ( <from></form>)

0

This code should do the trick:

function validateForm() {

    var user_type = document.getElementsByName('user_type');
    var u_type = '';

    for (var i = 0, length = user_type.length; i < length; i++) {
        if (user_type[i].checked) {
            // do whatever you want with the checked radio
            u_type = user_type[i].value;

            // only one radio can be logically checked, don't check the rest
            break;
        }
    }

    if (u_type == "") {
        alert("Select Who You are");
        return false;
    }
    var letter = document.getElementsByName('letter')[0].checked;

    if (letter == "" || letter == undefined) {
        alert("Select that you want to receive news letter");
        return false;
    }
}
0
source

Source: https://habr.com/ru/post/1626305/


All Articles