Skip to content

Instantly share code, notes, and snippets.

@haacked
Created April 10, 2011 17:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haacked/912552 to your computer and use it in GitHub Desktop.
Save haacked/912552 to your computer and use it in GitHub Desktop.
Sample jQuery File
<!DOCTYPE html>
<html>
<head>
<title>Index</title>
<script src="../../Scripts/jquery-1.5.1.js" type="text/javascript"></script>
<script src="../../Scripts/jquery.validate.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$("#form").validate({
rules: {
accountType: "checked"
},
messages: {
accountType: "You must select an account type"
}
});
});
</script>
</head>
<body>
<div>
<form id="form" method="post" action="/">
<label>Select an Account Type</label>
<input type="text" name="foo" class="required" />
<input type="radio" name="accountType" value="1" id="accountType_C" /><label for="accountType_C" >Coder</label>
<input type="radio" name="accountType" value="0" id="accountType_S" /><label for="accountType_S">Surreptitious Ninja</label>
<input type="submit" />
</form>
</div>
</body>
</html>
@philipproplesch
Copy link

I´m not quite sure, but you might try this:

<script type="text/javascript">
    $(function () {
        $("#form").validate({
            rules: {
                accountType:{ required:true }
            },
            messages: {
                accountType: "You must select an account type"
            }
        });
    });        
</script>

@rcagadoc
Copy link

I haven't tested this but the jQuery documentation recommends the following:

Forms and their child elements should not use input names or ids that conflict with properties of a form, such as submit, length, or method. Name conflicts can cause confusing failures.

With that said, try changing the ID given to your form to something else.

@brunokenj
Copy link

Haacked,

    $(function () {
        $("#form").validate({
            rules: {
                accountType: {
                    required: true
                }
            }
        });
    });

or... u use:

jquery.metadata.js
$.metadata.setType("attr", "validate");
and finally a attribute validate in first radio of group:
(input type="radio" name="accountType" value="1" id="accountType_C" validate="required:true" /)

@brunokenj
Copy link

With message:

    $(function () {
        $("#form").validate({
            rules: {
                accountType: {
                    required: true
                }
            },
            messages: {
                accountType: 'You must select an account type'
            }
        });
    });

@haacked
Copy link
Author

haacked commented Apr 10, 2011

Thanks all! I fixed it here: https://gist.github.com/912630

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment