Tuesday, June 4, 2024
 Popular · Latest · Hot · Upcoming
67
rated 0 times [  70] [ 3]  / answers: 1 / hits: 93692  / 12 Years ago, sat, september 15, 2012, 12:00:00

Possible Duplicate:

Check if inputs are empty using jQuery






I have form and textboxes, how will I determine if any of these textboxes is empty using javascript if else statement once a form button is clicked.



function checking() {
var textBox = $('input:text').value;
if (textBox == ) {
$(#error).show('slow');
}
}


Thanks in advance!


More From » jquery

 Answers
4

By using jQuery selectors for selecting the elements, you have a jQuery object and you should use val() method for getting/setting value of input elements.



Also note that :text selector is deprecated and it would be better to trim the text for removing whitespace characters. you can use $.trim utility function.



function checking() {
var textBox = $.trim( $('input[type=text]').val() )
if (textBox == ) {
$(#error).show('slow');
}
}


If you want to use value property you should first convert the jQuery object to a raw DOM object. You can use [index] or get method.



 var textBox = $('input[type=text]')[0].value;


If you have multiple inputs you should loop through them.



function checking() {
var empty = 0;
$('input[type=text]').each(function(){
if (this.value == ) {
empty++;
$(#error).show('slow');
}
})
alert(empty + ' empty input(s)')
}

[#83073] Thursday, September 13, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
desiraeleandrah

Total Points: 202
Total Questions: 111
Total Answers: 115

Location: Macau
Member since Mon, Nov 16, 2020
4 Years ago
;