Monday, 5 August 2013

Form validation using jQuery


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    <title></title>
<
script type="text/javascript" src="Scripts/jquery-1.4.1.min.js"></script
>
<script type="text/javascript">        $(document).ready(function () {
            $('#targets').submit(function () {
                var error = 0;
                var name = $('#name').val();
                if (name == '') {
                    error = 1;
                    $('#name_error_msg').html("Please enter name");
                    $('#name_error_msg').parent().show();
                }
                var country = $('#country').val();
                if (country == '0') {
                    error = 1;
                    $('#country_error_msg').html('Please select country');
                    $('#country_error_msg').parent().show();
                }
                var comment = $('#comment').val();
                if (comment == '') {
                    error = 1;
                    $('#comment_error_msg').html('Please enter comment');
                    $('#comment_error_msg').parent().show();
                }
                if (!($('#checkboxid').is(':checked'))) {
                    error = 1;
                    $('#checkboxid_error_msg').html("Please accept Terms & Condition");
                    $('#checkboxid_error_msg').parent().show();
                }
                if (error) {
                    return false;
                } else {
                    return true;
                }
            });
        });
</
script
>
</script><style type="text/css">        . label_col        {
            width: 20%;
            padding: 5px;
            float: left;
        }
        .field        {
            width: 75%;
            padding: 5px;
            float: left;
        }
        .error_field        {
            width: 75%;
            padding: 5px;
            float: left;
        }
        .error_msg        {
            display: none;
            clear: both;
            color: #f00;
        }
        li        {
            width: 100%;
            float: left;
        }
</
style
>
<body><form id='frmmain' runat="server" method='post'>    <ul>        <li>            <div class='label_col'>                Name</div>            <div class='field'>                <input type='text' name='name' id='name' />            </div>        </li>        <li class='error_msg'>            <div class='label_col'>            </div>            <div class='error_field' id='name_error_msg'>            </div>        </li>        <li>            <div class='label_col'>                Country</div>            <div class='field'>                <select name='test_field' id='country'>                    <option value='0'>Select</option>                    <option value='IN'D>India</option>                    <option value='AUS'>Australia</option>                    <option value='AME'>America</option>                </select></div>        </li>        <li class='error_msg'>            <div class='label_col'>            </div>            <div class='error_field' id='country_error_msg'>            </div>        </li>        <li>            <div class='label_col'>                Comment</div>            <div class='field'>                <textarea name='comment' id='comment'></textarea>            </div>        </li>        <li class='error_msg'>            <div class='label_col'>            </div>            <div class='error_field' id='comment_error_msg'>            </div>        </li>        <li>            <div class='label_col'>            </div>            <div class='field'>                <input type='checkbox' name='test_field' id='checkboxid' />               I Agree
            </div>        </li>        <li class='error_msg'>            <div class='label_col'>            </div>            <div class='error_field' id='checkboxid_error_msg'>            </div>        </li>        <li>            <div class='label_col'>            </div>            <div class='field'>                <input type='submit' value='Save' /></div>        </li>    </ul>    </form
>
</body>
</html.