Try something like this...

$('form#checkout').submit( function(){
        //Refernce the elm if you are going to $() it more then once
        $elm = $('input#rxt_forename'); 
        if($elm.val() == ''){
                //Double check the end() usage here, should work
                
elm.addClass("required").siblings("span.error").show().end().get(0).focus();
                return false;
        }else{
                $elm.removeClass("required").siblings("span.error").hide();
        }
});

-ALEX

________________________________________
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Luc Pestille
Sent: Wednesday, March 21, 2007 9:40 AM
To: jQuery Discussion.
Subject: [jQuery] Code simplifying request.


Hi all,
I've got some simple validation to do (not enough to use one of the form 
plugins), and have this;
 
// when the form is submitted
  $('form#checkout').submit( function(){
  
   // if field has something in it, do submit actions, otherwise write out error
   if( $('input#rxt_forename').val() == '' ){
    // add class to change colour
    $('input#rxt_forename').addClass("required").get(0).focus();
    $('input#rxt_forename').siblings("span.error").show();
    return false;
   }else{
    $('input#rxt_forename').removeClass("required");
    $('input#rxt_forename').siblings("span.error").hide();
   }
 
How can i simplify it? I would have thought replacing the input selector with 
$(this) would work, but it doesn't, and I'm sure the class/siblings processes 
could be chained...
 
Thanks,
 
 


Luc Pestille
Web Designer 
e: [EMAIL PROTECTED]
t: +44 (0)1628 899 700
f: +44 (0)1628 899 701 
 


In2
Thames House
Mere Park
Dedmere Road
Marlow
Bucks
SL7 1PB

Tel 01628 899700
Fax 01628 899701
e: [EMAIL PROTECTED]
i: www.in2.co.uk

This message (and any associated files) is intended only for the use of 
discuss@jquery.com and may contain information that is confidential, subject to 
copyright or constitutes a trade secret. If you are not discuss@jquery.com you 
are hereby notified that any dissemination, copying or distribution of this 
message, or files associated with this message, is strictly prohibited. If you 
have received this message in error, please notify us immediately by replying 
to the message and deleting it from your computer. Messages sent to and from us 
may be monitored. Any views or opinions presented are solely those of the 
author [EMAIL PROTECTED] and do not necessarily represent those of the company. 


_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/

Reply via email to