开发者

adding Form Validation Javascript of jquery?

How can I add inline Validation to make sure a choice of radio input must be selected

<script type="text/javascript">
    function choosePage() {
      开发者_如何转开发  if(document.getElementById('weightloss').form1_option1.checked) {
            window.location.replace( "http://google.com/" );
        }
        if(document.getElementById('weightloss').form1_option2.checked) {
            window.location.replace( "http://yahoo.com/" );
        }
    }
</script>

<form id="weightloss">
     <input type="radio" id="form1_option1" name="weight-loss" value="5_day" class="plan">
     <label for="form1_option1"> 5 Day - All Inclusive Price</label><br>
     <input type="radio" id="form1_option2" name="weight-loss" value="7_day">
     <label for="form1_option2"> 7 Day - All Inclusive Price</label><br>
     <input type="button" value="Place Order" alt="Submit button" class="orange_btn" onclick="choosePage()">
</form>


you are using document.getElementById('weightloss') but infact there is no any element which has id 'weightloss'.

Try with this

<script type="text/javascript">
    function choosePage() {
        if(document.getElementById('form1_option1').checked) {
            window.location.replace( "http://google.com/" );
        }
        if(document.getElementById('form1_option2').checked) {
            window.location.replace( "http://yahoo.com/" );
        }
    }
</script>

Hope it will help you.


You just had to make js evaluate the form first.

<script type="text/javascript">
    function choosePage() {
        if(document.forms["weightloss"]["form1_option1"].checked == true) {
            window.location.replace( "http://google.com/" );
        }
        if(document.forms["weightloss"]["form1_option2"].checked == true) {
            window.location.replace( "http://yahoo.com/" );
        }
        else { alert('Please choose an option'); } // for inline alerts see below.
    }
</script>

For inline text alerts try the following:

    else { my_message.innerHTML = "Please choose an option" }

Add an element to your page to contain the message:

    <p id="my_message"></p> <!-- when the form submits empty your message will appear here.
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜