Jump to content

Submit Validation


CONFUSIONUK

Recommended Posts

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"  :confused:

 

$(".submit").click(function() {
  if(!$.trim(".message").length){
    alert("Please enter a message!");
  }
  else(messageProcess)
});

 

 

Any help will be much appreciated  :)

Link to comment
https://forums.phpfreaks.com/topic/217803-submit-validation/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/217803-submit-validation/#findComment-1130720
Share on other sites

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>

Link to comment
https://forums.phpfreaks.com/topic/217803-submit-validation/#findComment-1131017
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.