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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.