0

i'm designing a form which gets user information and then go another page. There's lots of "input text" controls to fill. User must fill all "input text" controls in order to go home page. if a control is empty, an "x" icon should be put inside "input text".

My main problem is, i'm new at jquery and i dont want to write this function for every control:

$('#send').click(function () {
            if ($('#kname').val().length == 0) {
                $('#kname').css({ background: "url(image/error.png) no-repeat right" });
            }
        });

i need a general function for all controls. here is my html lines:

   //some code
       <input type="text" id="knumber" />
        <input type="text" id="kname" />
        <input type="text" id="ksurname" />
   //some other code

edit: i'm using an "input button" to send these info.

<input type="button" value="Send Them" id="send" />

5 Answers 5

3

What about using $.each and looping through all the text elemnts

$('#send').click(function () {
       $("input[type=text]").each(function(){
            if ($(this).val() == '') {
                $(this).css({ background: "url(image/error.png) no-repeat right" });
            }
        });
Sign up to request clarification or add additional context in comments.

Comments

1

Try to validate all input elements with type text as given below

$("input[type=text]").filter(function() {
    return $.trim(this.value).length == 0
}).css({
    background : "url(image/error.png) no-repeat right"
});

or add a class to those elements which you wish to validate like not-empty then

<input id="kname" type="text" class="not-empty" />
<input id="ksurname" type="text" class="not-empty" />

then

$(".not-empty").filter(function() {
    return $.trim(this.value).length == 0
}).css({
    background : "url(image/error.png) no-repeat right"
});

1 Comment

@AliSağırvelioğulları better if you delete the other question
0

try using this add a class name "requiredField" to all your text fields

in your button click method find all the textboxes

textboxes = $('#formid').find('.requiredField');

then use each method.

textboxes.each(function() {
 if(this.value.length==0){
   $('#kname').css({ background: "url(image/error.png) no-repeat right" });
    //do something here
}
}

2 Comments

should i write this method into my button's click event?
@AliSağırvelioğulları yes, i mentioned it in the second line
0

how about?

$("#send").click(function() {
     $("input[type=text]").each(function(){
          if ($(this).val().length == 0) {
               $(this).css({ background: "url(image/error.png) no-repeat right" });
          }
     });
});

example: http://jsfiddle.net/ddDSB/

Comments

0
//some code
<input class="validate" type="text" id="knumber" />
<input class="validate" type="text" id="kname" />
<input class="validate"type="text" id="ksurname" />
//some code...


$(".validate").click(function(){
    ...
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.