Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate if textbox value is empty in a series of textboxes?

There are a series of textboxes like:

 <input  type="text" class="jq-textBox" />
 <input  type="text" class="jq-textBox" />
 <input  type="text" class="jq-textBox" />
 <input  type="text" class="jq-textBox" />
 <input  type="text" class="jq-textBox" />

User can fill up the textbox values from top to bottom order. Only first textbox is required and all other textboxes are optional.

Allowed order to fill textbox values:

1st
1st & 2nd
1st, 2nd & 3rd
and likewise in sequence order

Dis-allowed order:

2nd
1st & 3rd
1st, 2nd & 4th    

This means that user needs to fill up the first textbox only or can fill up the other textboxes in sequential order. User can NOT skip one textbox and then fillup the next one.

How to validate this in javascript/jQuery?

Any help is highly appreciated!

like image 929
user2338652 Avatar asked Oct 04 '13 13:10

user2338652


People also ask

How to check if the textbox is empty or not in JavaScript?

We can use the TextBox.Text.Length == 0 condition inside the if statement to check if the text box is empty or not. See the following code example. In the above code, we checked whether the text box is empty or not with the TextBox.Text.Length property in C#.

How to check if all textboxes in specified userform are empty in Excel?

The below VBA code can help you to check if all textboxes in a specified userform are empty or not in Excel. Please do as follows. 1. Press the Alt + F11 keys to open the Microsoft Visual Basic for Applications window.

How to check textboxes in a form using VBA code?

In the Microsoft Visual Basic for Applications window, click Insert > Module. Then copy below VBA code into the code window. Note: In the code, UserForm1 is the name of the userform which contains the textboxes you will check.

Is text box empty if there are only whitespaces inside?

This approach also takes the whitespaces into consideration and displays the error message TEXT BOX IS EMPTY if there are only whitespaces inside the text box. The TextBox.Text.Length property gets the length of the text inside the text box in C#.


1 Answers

I would personaly use the disabled html attribute.
See this jsFiddle Demo

html

<form>
    <input  type="text" class="jq-textBox" required="required" />
    <input  type="text" class="jq-textBox" disabled="disabled" />
    <input  type="text" class="jq-textBox" disabled="disabled" />
    <input  type="text" class="jq-textBox" disabled="disabled" />
    <input  type="text" class="jq-textBox" disabled="disabled" />
    <input type="submit" />
</form>

(Note the required attribute for HTML5)

jquery

$('input.jq-textBox').on('keyup', function(){
    var next = $(this).next('input.jq-textBox');
    if (next.length) {
        if ($.trim($(this).val()) != '') next.removeAttr('disabled');
        else {
            var nextAll = $(this).nextAll('input.jq-textBox');
            nextAll.attr('disabled', 'disbaled');
            nextAll.val('');
        }
    }
})

Also see nextAll() jquery Method

Edit : If you want to hide the disabled inputs in order to show them only when the previous input is filled, just add this css :

input[disabled] {
    display: none;
}

Demo

like image 71
Brewal Avatar answered Oct 13 '22 02:10

Brewal