CONFUSIONUK Posted November 5, 2010 Share Posted November 5, 2010 This was a jQuery question I posted up on the jQuery forums, but they seem to be pretty rubbish at getting back to you :-\ Also I know it's not coded correctly I have just written the principles of what I'm trying to achieve if you get me I want when the user clicks on the button with the class of ".submit" to first check the text field ".message" to see if it has any input at all and if none display the alert, if there is input then run the process function "messageProcess" $(".submit").click(function() { if(!$.trim(".message").length){ alert("Please enter a message!"); } else(messageProcess) }); Any help will be much appreciated Quote Link to comment Share on other sites More sharing options...
michaellunsford Posted November 5, 2010 Share Posted November 5, 2010 looks good to me. Does it not work? Quote Link to comment Share on other sites More sharing options...
CONFUSIONUK Posted November 5, 2010 Author Share Posted November 5, 2010 Nope I believe it is because the first if statement is completely wrong, I just want it check the the field after it has been trimmed (to see if it is empty) but I'm not sure how to do it Quote Link to comment Share on other sites More sharing options...
Adam Posted November 5, 2010 Share Posted November 5, 2010 jQuery.trim() takes a string input, not a selector. Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 5, 2010 Share Posted November 5, 2010 I don't use JQuery so I don't know if I am reading that code right, but I don't think you want to be attaching the validation to the onClick event of the submit button. Instead you should be attaching the validation to the submit event of the form. Otherwise, someone could submit the form using the enter key and the validation would not take place. Quote Link to comment Share on other sites More sharing options...
michaellunsford Posted November 5, 2010 Share Posted November 5, 2010 very good point, mjdamato. I had focused on the code and overlooked the general best practice, but reading it now I wholeheartedly agree. Quote Link to comment Share on other sites More sharing options...
CONFUSIONUK Posted November 6, 2010 Author Share Posted November 6, 2010 I'm happy to try work on a new way about making this, any snippets people can throw in that will make this safer etc? Quote Link to comment Share on other sites More sharing options...
Adam Posted November 6, 2010 Share Posted November 6, 2010 You can just use .submit(). Also note that I've switched it to use an ID, as you can have multiple elements with the same class: <form name="formName" action=""> <input type="text" name="message" id="message" /> <input type="submit" /> </form> <script type="text/javascript"> $('form[name=formName]').submit(function() { var message = $('#message').val(); if (!$.trim(message)) { // problem } }); </script> Quote Link to comment Share on other sites More sharing options...
CONFUSIONUK Posted November 7, 2010 Author Share Posted November 7, 2010 That looks like it may well do the job. Will try it out in the morning and get back to you if anything else gets in the way thanks Quote Link to comment Share on other sites More sharing options...
CONFUSIONUK Posted November 8, 2010 Author Share Posted November 8, 2010 That works an absolute treat Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.