views:

101

answers:

4

All i want to do is to check if the textfield has been changed. if not, i want to highlight the boxes when submit is pressed. how do i do that? seems very simple, but not sure why every other solution is so complicated

+2  A: 

You can use the jQuery Validate plugin.

SLaks
ooo! New version! 1.7 is jQuery 1.42 compatible.
Mark Stahler
+1  A: 

Bassicly, you just say it has never been changed, and when it cahnges, you set a flag saying is has been changed:

var changed = false;
$('#textfield').change(function(){
    changed = true;
});
if(changed){
    $('.textbox').each(function(){
        $(this).addClass('.highlighted');
        //or something like this, whatever you want to do to highlight them
    });
}
Pim Jager
+4  A: 

Building upon Pim's answer, you can associate the changed flag for each text field using jQuery's data API.

// initially, assign changed to false for all text fields
$("input:text").data("changed", false);

// if any field changes, set its changed flag to true
$("input:text").change(function() {
    $(this).data("changed", true);
}

// finally on submission, get all text fields that did not
//  change by checking their "changed" property
var unchangedItems = $("input:text").filter(function() {
    return $(this).data("changed") === false;
});
Anurag
way better then my global flag idea. nice solution!
Pim Jager
be aware that changing an input's value with javascript doesn't fire the `changed` event
Andrew Bullock
@Andrew Bullock - yes, but you CAN put a $('fieldIChanged').trigger("change"); on it when you DO change it that way :) where 'fieldIChanged' is the selector for that item (this?)
Mark Schultheiss
@Mark really? oo ill investigate that
Andrew Bullock
A: 

DEMO: http://jsbin.com/aceze/29

$(function() {
        $('#myform').submit(function(){ 
          var error = false;
          $('#myform > .validate').each(function(){
            if ( this.value == 'fill this input' || this.value == '') {
              $(this).css('border','1px solid red');
              error = true;
            } else {
           $(this).css('border','1px solid green'); 
         }
    });
       if ( error == false ) {
          // success
       } else {
       // error  
     }
       return false;
     });
});​

HTML

<form id="myform" methot="post" action="#">  
  <input class="validate" type="text" value="fill this input" name="input"/>    <br />  
  <input type="submit" value="submit" name="submit"/>
</form>

Give each Input you want validate the .validate class!

aSeptik
he asked for changed not for isEmpty
Ghommey
`this.value == ''` is equal to `this.value == 'some text'` assuming each input have `some text` as value! althoght this is a proof of concept and it is what the OP asked, check after submission not onChange!!!
aSeptik