Hi Jörn and others jquery forum readers, So, is there anything wrong with the script? It still doesn't work when email field is empty; fine with both validating the field when there is input. onblur=alert($("#field").valid()) gives me 0 when empty.
$(document).ready(function(){ $("#myform").validate({ rules: { field: { email: true } } }); }); <form id="myform"> <label for="field">Required, email: </label> <input class="left" id="field" name="field" onblur=alert($ ("#field").valid()) /> <br/> <input type="submit" value="Validate!" /> </form> On Apr 12, 8:33 pm, NobitaNobi79 <frankcheun...@gmail.com> wrote: > It still doesn't validate when the field is empty. Yes, I removed " > required: true, " in > > $("#myform").validate({ > rules: { > field: { > required: true, > email: true > } > } > > }); > > The code is exactly the same just without the required: true, line as > belows. > And if I modified the <input id="field" name="field" /> to <input > class="left" id="field" name="field" onblur=alert($("#field").valid > ()) /> It alerts 0 when it's empty, 0 when it's invalid email, and 1 > when it's valid email address. I'm expecting 1 when field is empty. > > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" > "http://www.w3.org/TR/html4/loose.dtd"> > <html> > <head> > <script src="http://code.jquery.com/jquery-latest.js"></script> > <script type="text/javascript" src="http://dev.jquery.com/view/trunk/ > plugins/validate/lib/jquery.delegate.js"></script> > <script type="text/javascript" src="http://dev.jquery.com/view/trunk/ > plugins/validate/jquery.validate.js"></script> > <script type="text/javascript"> > jQuery.validator.setDefaults({ > debug: true, > success: "valid"});; > > </script> > > <script> > $(document).ready(function(){ > $("#myform").validate({ > rules: { > field: { > email: true > } > }}); > > }); > </script> > <style>#field { margin-left: .5em; float: left; } > #field, label { float: left; font-family: Arial, Helvetica, sans- > serif; font-size: small; } > br { clear: both; } > input { border: 1px solid black; margin-bottom: .5em; } > input.error { border: 1px solid red; } > label.error { > background: > url('http://dev.jquery.com/view/trunk/plugins/validate/ > demo/images/unchecked.gif') no-repeat; > padding-left: 16px; > margin-left: .3em; > } > label.valid { > background: > url('http://dev.jquery.com/view/trunk/plugins/validate/ > demo/images/checked.gif') no-repeat; > display: block; > width: 16px; > height: 16px; > } > </style> > </head> > <body> > > <form id="myform"> > <label for="field">Required, email: </label> > <input class="left" id="field" name="field" /> > <br/> > <input type="submit" value="Validate!" /> > </form> > > </body> > </html> > > On Apr 12, 3:36 am, Jörn Zaefferer <joern.zaeffe...@googlemail.com> > wrote: > > > Just remove the required-rule. All other methods include a check to > > make a field optional when empty. > > > Jörn > > > On Sat, Apr 11, 2009 at 7:16 PM, NobitaNobi79 <frankcheun...@gmail.com> > > wrote: > > > > Hi, I am new to jQuery. I follow the example on > > >http://docs.jquery.com/Plugins/Validation/Methods/emailtoadd rules > > > to a form validator. When I modified the rules to email: true > > > (required: false), the script seems to stall there, no error message > > > nor a check. Can someone tell me if I am doing anything wrong here? > > > What I want is to let the validation pass if the optional fields are > > > valid when they are filled or they are left out empty. > > > > I can, of course add an extra script like... > > > if (($("#field").val() == "") || $("#field").valid()) { > > > optionalValid = true; > > > } > > > But it seems very redundant considering the rules: { field: {email: > > > true, required: false}}, with or without the default required: false > > > option. > > > > Thanks