grahamgr3 Posted June 20, 2014 Share Posted June 20, 2014 The following code is from chapter 5 of this book. if (!comments || !comments.value || (comments.value.indexOf('<') != -1) ){ okay = false; alert ('Please enter your comments, without any HTML!'); My question is why do we have to both put !comments and also !comments.value. Aren't they the same?? Why check comments if it doesn't have a value. I just learned php with Larry's book, and am new at javascript. Link to comment Share on other sites More sharing options...
HartleySan Posted June 20, 2014 Share Posted June 20, 2014 Basically, you never want to assume anything in programming. In this case, you're ensuring that comments is not null (i.e., it's a valid DOM element) and that it has the value property (i.e., it's a valid form input element). If you assume both of those things without checking, then your code would through an error on the comments.value.indexOf part of the if statement whenever comments was not set to a DOM input element. That make sense? Link to comment Share on other sites More sharing options...
Elizabeth_Keen Posted June 24, 2014 Share Posted June 24, 2014 Go through the below links for more knowledge about Javascript form validation. http://www.w3schools.com/js/js_form_validation.asp http://www.javascript-coder.com/html-form/javascript-form-validation.phtml Hope it helps. For More Web Based Applications Visit Us At: http://www.ati-erp.com Link to comment Share on other sites More sharing options...
HartleySan Posted June 24, 2014 Share Posted June 24, 2014 Elizabeth, welcome to the forums. You're more than welcome to post here, but please do not advertise like that. Thanks. Link to comment Share on other sites More sharing options...
Necuima Posted June 25, 2014 Share Posted June 25, 2014 Just a general comment that there is also a powerful (and free) jQuery form validation plug-in that can be used. Cheers from Oz, Necuima. Link to comment Share on other sites More sharing options...
Recommended Posts