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

  • Home
  • SEARCH
  • 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 8376647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:28:38+00:00 2026-06-09T15:28:38+00:00

I wrote this simple script for validating non-empty input fields. When, for instance, the

  • 0

I wrote this simple script for validating non-empty input fields.

When, for instance, the form is sent and the input with the id ‘name’ is empty,
a short message will be displayed next to that element. The problem is that if
the form is sent one more time and that input is not empty anymore, doing this
$(element).nextAll().remove() will cause that the element with the id ‘phone’ disappears.

I just need to remove the message error and not all the other elements.

Please, suggest a short and easy way to solve that problem.

<!DOCTYPE html>
<html>
    <head>
        <script type="text/javascript"
        src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js">
        </script>
        <script type="text/javascript">
            $(document).ready(function(){
                $("#formu").submit(function(){
                     validateInputText($("#name"),"Enter you name");
                     validateInputText($("#phone"),"Enter your phone number");    
                     return false;                
                });
                function validateInputText(inputText, errorMessage){
                    if($(inputText).val()==''){
                        setFieldAsNotValid(inputText, errorMessage)
                    }else{
                        removeErrorMessage(inputText)
                    }
                }

                function setFieldAsNotValid(element, errorMessage){
                    $(element).nextAll().remove();
                    $(element).after('<b>'+errorMessage+'</b>')
                    $(element).addClass('selected');
                }

                function removeErrorMessage(element){
                    $(element).nextAll().remove();
                    $(element).removeClass('selected');
                }       
            });



        </script>
    </head> 
    <body>
       <form id="formu">
           name: <input type="text" id="name" />
           phone: <input type="text" id="phone" />
           <input type="hidden">
           <input type="submit">
        </form>
    </body>

</html>
  • 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-09T15:28:39+00:00Added an answer on June 9, 2026 at 3:28 pm

    I created some code for you. It is not a direct answer for your question, but I think It is a better way to do what you want.

    HTML:

    <form id="myForm">
        Name: <input id="name" type="text" allowblank="false" max="10"/><span for="name" class="errorMessage"></span>
        <br/>
        Age: <input id="age" type="text" allowblank="false"/><span for="age" class="errorMessage"></span>
        <br/>
        <input type="submit" value="Save"/>
    </form>
    

    CSS:

    .error {
        border-color: #ff0000;
    }
    
    .errorMessage {
        color: #ff0000;
    }
    

    JavaScript:

    $( "#myForm" ).submit(function(){
        return validate( $(this) );
    });
    
    function validate( $form ) {
    
        var isValid = true;
    
        $textInputs = $form.find( "input[type=text]" );
        $textInputs.each(function( index, item ){
    
            var isThisValid = true;
            var $item = $(item);
            var $errorSpan = $( "span[for=" + $item.attr("id") + "]" );
    
            var max = parseInt( $item.attr( "max" ) );
    
            if ( $item.attr( "allowblank" ) == "false" ) {
                if ( $item.val().trim() == "" ) {
                    $item.addClass( "error" );
                    $errorSpan.html( "this field cannot be empty" );
                    isThisValid = false;
                }
            }
    
            if ( max && isThisValid ) {
                if ( $item.val().trim().length > max ) {
                    $item.addClass( "error" );
                    $errorSpan.html( "the maximum length of this field is " + max );
                    isThisValid = false;
                }
            }
    
            if ( isThisValid ) {
                $item.removeClass( "error" );
                $errorSpan.html( "" );
            }
    
            if ( isValid && !isThisValid ) {
                isValid = false;
            }
    
        });
    
        return isValid;
    
    }
    

    With this code, you can improve your validation, creating new rules. There are some jQuery plugins that do the job, but since the plugin site of jQuery is under maintenance, I think you can use this approach. Note that I used some custom attributes to set the validation type, but you can use another approach, as creating a object that contains the validation constraints. I used “max” instead of maxlength since the latter is a valid input tag attribute. Anyway, the jsFiddle is here: http://jsfiddle.net/davidbuzatto/2N4yY/

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

Sidebar

Related Questions

I wrote a simple script that add extra form fields when needed by the
I wrote a simple script for validating login form as follows: <script type=text/javascript> function
I'm trying to retrieve folders name from a zip file. I wrote this simple
I wrote a simple Linux Script this way export JAVA_HOME=/usr/local/jdk1.6.0_20 export PATH=/usr/local/jdk1.6.0_20/bin LIB_DIR=/home/praveen/lib export
I wrote this simple script that reads a text file and then adds the
I'm trying to modify this very simple shell script I wrote to check if
This is a very simple bash script I wrote: #!/bin/bash ITEM_LIST=items.txt LOG_FILE=log.log TOTAL_ITEMS=$(wc -l
I wrote a simple script for validating forms in jQuery. These are the submit
I wrote this simple C code and compiled it using Visual Studio 2010, with
So I wrote this simple console app to aid in my question asking. What

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.