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 7172685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:39:57+00:00 2026-05-28T15:39:57+00:00

I’m super new to this language. I have the ability to check for the

  • 0

I’m super new to this language. I have the ability to check for the label sting but I want to make sure that the input field next to it has at least 6 digits (numerical if possible)

$(document).ready(function(){
    $("label:contains('6 digit')") //i get lost here
});

Markup:

<form>
    <label>Test</label><input value="1" type="text" class="check">
    <label>Test</label><input value="1" type="text" class="check">
    <label>Test</label><input value="1" type="text" class="check">
    <label>6 digit</label><input value="1" type="text" class="check"> 
    <label>6 digit</label><input value="1" type="text" class="check">
    <label>Test</label><input value="1" type="text" class="check">

    <input type="submit">
</form>
  • 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-05-28T15:39:58+00:00Added an answer on May 28, 2026 at 3:39 pm

    If at all possible, find an easy way to distinguish the fields you need to validate, like Tim Bolton suggested with ID attributes on the elements, or perhaps by CSS Classes like below (i.e., the “needs6” class):

    <form>
        <label>Test</label><input value="1" type="text" class="check">
        <label>Test</label><input value="1" type="text" class="check">
        <label>Test</label><input value="1" type="text" class="check">
        <label>6 digit</label><input value="1" type="text" class="check needs6"> 
        <label>6 digit</label><input value="1" type="text" class="check needs6">
        <label>Test</label><input value="1" type="text" class="check">
        <input type="submit" />
    </form>
    

    Then your jQuery selector becomes much simpler (i.e., $("input.needs6")).

    Once you have your inputs selected, there are many ways to validate them.
    Personally, I would use the following method:

    $(document).ready(function() {
        var testFor6 = function () {
            var allHave6 = true;
            $("input.needs6").each(function () {
                var input = $(this);
                var value = input.val();
                if (!value.match(/\d{6}/g)) {
                    allHave6 = false;
                    input.addClass('doesNotHave6');
                } else {
                    input.removeClass('doesNotHave6');
                }
            });
    
            return allHave6;
        };
        $('input[type="submit"]').click(function (e) {
            var allHave6 = testFor6();
            if (!allHave6) {
                e.preventDefault(); //Cancel event-bubbling.
            }
            return allHave6;        //Prevent form submission when false.
        });
    });
    

    If you want to see this in action, pop it into http://jsfiddle.net/ and test it, although you may want to combine this with some sample CSS classes to see it working. Samples below.

    .needs6 {background-color: yellow;}
    .doesNotHave6 {background-color: red;}
    

    Hope this helps,

    Pete

    Addendum:

    Do you mean spaces…

    <form>
        <label>Test</label><input value="1" type="text" class="check">
        <label>Test</label><input value="1" type="text" class="check">
        <label>Test</label><input value="1" type="text" class="check">
        <!-- here? --><label><!-- here? -->6 digit</label><!-- here? --><input value="1" type="text" class="check needs6" value="<!-- here? -->"> 
        <label>6 digit</label><input value="1" type="text" class="check needs6">
        <label>Test</label><input value="1" type="text" class="check">
        <input type="submit" />
    </form>
    

    Please clarify.


    Certainly!

    Simply change the input loop function to any portion of the following…

    $("input.needs6").each(function () {
        var input = $(this);
        var value = input.val();
        //You can use this to trim.
        var trimmedValue = value.replace(/\s\s*$/, '').replace(/^\s\s*/, '');
        //You can use this to find leading whitespace.
        var hasLeadingSpaces = value.match(/^\s\s*/, '');
        //You can use this to find trailing whitespace.
        var hasTrailingSpaces = value.match(/\s\s*$/, '');
        if (hasLeadingSpaces) {
            //Do something
        }
        if (hasTrailingSpaces) {
            //Do something
        }
        //Check original value
        if (!value.match(/\d{6}/g)) {
            allHave6 = false;
            input.addClass('doesNotHave6');
        } else {
            input.removeClass('doesNotHave6');
        }
        //Check trimmed value
        if (!trimmedValue.match(/\d{6}/g)) {
            allHave6 = false;
            input.addClass('doesNotHave6');
        } else {
            input.removeClass('doesNotHave6');
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want to count how many characters a certain string has in PHP, but
this is what i have right now Drawing an RSS feed into the php,
I have a small JavaScript validation script that validates inputs based on Regex. I
I have this code to decode numeric html entities to the UTF8 equivalent character.
I want use html5's new tag to play a wav file (currently only supported
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.