Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9115193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:22:21+00:00 2026-06-17T04:22:21+00:00

I tried this code to validate radio buttons: <html> <head> <script src=jquery-1.8.3.js type=text/javascript> </script>

  • 0

I tried this code to validate radio buttons:

<html>
<head>
<script src="jquery-1.8.3.js" type="text/javascript">
</script>
<script src="jquery.validate.js" type="text/javascript">
</script>
<script>
$(document).ready(function(){
$("#myform").validate({
rules:{
username:{
required:true,
alphanumeric:true
}
gender:"required",
},
messages:{
username:
{
required:"username cannot be empty",
alphanumeric:"username needs to be alphanumeric"
}
gender:{
required:"specify this field",
}
}
});
});


</script>
<style type="text/css">


label.error
{
color:red;
padding-left:.5em;
}
</style>

</head>
<body>
<form id="myform" action="r.html">
<table>

<tr>
<td><label for="username">Username<em>*</em>:</td></label><td><input type="text"           id="username" name="username"/></td>
</tr>

<tr>
<td><label for="gender" validate="required:true" class="required">Gender<em>*</em>:      </label></td><td><input type="radio" id="gender" name="gender" value="male"/>Male</td>
</tr>
<tr>
<td></td><td><input type="radio" id="gender" name="gender" value="female"/>Female</td>
</tr>

<tr>
<td></td>
<td><input type="submit" value="submit" id="submit"/></td>
</tr>
</table>
</form>
</body>
</html>

when I tried this code, gender field is being validated but error message is appearing in-between gender and male attributes..but it must appear 0.5em after male attribute.

and alphanumeric validation is not working at all.
please someone help me out.
Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T04:22:22+00:00Added an answer on June 17, 2026 at 4:22 am

    You have a multitude of errors….


    Just like in your other question, this is not the typical syntax for a rule…

    gender:"required",
    

    It should be required: true and inside of your rules: {} like this…

    rules: {
        username: {
            required: true,
            alphanumeric: true
        },
        gender: {
            required: true
        }
    },
    

    In your HTML here…

    <label for="gender" validate="required:true" class="required">Gender<em>*</em>:</label>
    

    validate="required:true" is totally superfluous and should be removed.

    • It’s meaningless as it’s a totally fabricated attribute.
    • It’s applied to the label instead of the input.

    class="required" is not only superfluous since it’s applied to a label and not an input element, it’s totally redundant because you’re already specifying the rule within your .validate() options.

    With the rule defined in your jQuery, the HTML would simply be:

    <label for="gender">Gender<em>*</em>:</label>
    

    To use the alphanumeric rule, you need to also include the “additional methods” file here:

    http://ajax.aspnetcdn.com/ajax/jquery.validate/1.10.0/additional-methods.js

    or minified:

    http://ajax.aspnetcdn.com/ajax/jquery.validate/1.10.0/additional-methods.min.js


    You have invalid HTML here. Note how </label> wraps outside the </td>…

    <td><label for="username">Username<em>*</em>:</td></label>
    

    Should be:

    <td>
        <label for="username">Username<em>*</em>:</label>
    </td>
    

    As far as placing your errors, I would not have used a table for laying out the form in the first place. Be that as it may, you could place the error messages using the built-in errorPlacement: method. error.insertAfter(element) is the default function.

    Using conditionals to test the type of input will allow you to change this function for the radio buttons. You can tweak the code below to place your messages as desired.

    jQuery .validate() method:

    $("#myform").validate({
    
        // your rules and options,
    
        errorPlacement: function (error, element) {
            if (element.is('input[type="text"]')) {
                error.insertAfter(element); // placement of errors on text fields
            } else if (element.is('input[type="radio"]')) {
                element.next('.radio-message').html(error);  // placement of errors on radio buttons
            } else {
                error.insertAfter(element);  // default placement
            }
        }
    });
    

    HTML:

    <input type="radio" id="gender" name="gender" value="male" />Male <span class="radio-message"></span>
    

    Working Demo:

    http://jsfiddle.net/kP4Xy/


    You were also missing a comma (will break code) and had some extra commas (might break code in IE).

    Again, like I stated before, when you follow proper code indentation and formatting, it’s much easier to see these mistakes. I strongly suggest you take my advice this time. You will become a better programmer, your code will be easier to read, your HTML will be more valid, and your JavaScript will be easier to troubleshoot.


    Plugin Documentation:

    http://docs.jquery.com/Plugins/Validation

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a HTML script like this: <form id=no1> <input id=title type=text /> <input
I'm using the jQuery validate plugin. I have this code: $(.btn-overview-basic-save).click(function(e) { if (!$('.contact-overview').valid())
I tried this code to open a file in Python: f = open("/Desktop/temp/myfile.txt","file1") It
i have tried this code to redirect a php page.but it s not working
how to make php can read log file in /var/log/.... I tried this code
I am trying to enable mod_deflate. I have Apache 2.0+ and tried this code
I tried with this code to post on the wall (Twitter) of a user
i've tried using this code and this to make a random quote generator, but
How do I check if a textarea contains nothing? I tried with this code:
I tried to run this code: #define ROW_CNT 8; #define COLUMN_CNT 24; #define FIRST_COLUMN

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.