I want it so that when I click the submit button #submit, it starts the function to find the information from the input field and alerts me the value. However I can’t seem to get it to work. What am I missing? It says it is undefined when it alerts
Derp, yea you are right. So now that I can find the value of the field, is there a way to pass this off to a function that validates whether or not this is a valid email address, either via PHP or jquery?
Yes, you can test the string format in JavaScript using a basic form of an email address, google “simple javascript regex email” for examples - or you can make an ajax request to the server.
You’ll want to prevent the form from submitting if you need to validate.
$("#submit").click(function(event){
...
if (invalid) {
event.preventDefault();
}
});
<script type="text/javascript">
$(function(){
$("#errorMessage").hide();
$("#submit").click(function(){
var validateEmail = (".enter-information").val();
if(validateEmail==""){
$("#errorMessage").fadeIn(800);
$(".enter-information").focus();
return false;
}
};
});
</script>
This only works if the field is left blank. Why isn’t this working? It’s supposed to hide the error message when the page loads but it doesn’t. I realize I could do this in CSS but its bugging me. The goal of this is that I used to have a form that would leave the page when I submitted, so I wanted to include this jquery that returns false when there is a mistake so that it doesn’t send the form
One piece of advice: do not ever name any element using a reserved word; in this case ‘submit’. I have had this issue many times before someone pointed out to me that naming a submit button ‘submit’ confuses JS. Rename it to something like ‘submitBtn’ (and, of course, all your code for it) and see if that clears anything up.
although, if there’s only one submit button in the form, there is no need to give the submit button a name in the first place. your server-side code should always verify the data it expects, not assuming all data are present just because some arbitrary parameter exists.
You need to put all your JS files before </body>. That’s where all Javscript should go (99% of it).
Your JS is executing before the HTML is even loaded. So it won’t take. Put the JS right before </body> so the HTML is loaded and it actually has code to work off of.
Nope, didn’t go in, I’ll explain on sunday. That makes sense though, I was lookin at my code and was like, there is nothing wrong with it, wtf. Makes sense though. Now that this can work (i assume it works, havent tried yet) I can try to use Ajax. I’m teaching myself with a tutorial. I coded this part by myself fine, jquery isn’t really all that hard once you memorize the selectors and syntax. Ajax is newish to me
Just tried doing that. still doesn’t work. here is code
Your code looks right, so that’s what I theorized. Looked at your code and sure enough your JS was in <head>. Should work if you change it.
FYI you should do on submit, not on click. Not everyone will click the submit. A lot of people will jsut hit enter.
Try this. Untested. You need to prevent the initial submission. Also you were missing a $ before .enter-information. Also, I prefer putting $ before variables in jQuery to distinguish jQuery vars from regular JS variables.
You forgot a parenthesis after the if statement ends. But it still doesn’t work. Also what were you talking about having a $ before .enter-information? I’m not sure what you’re talking about. I also didn’t see a reference to it in your new code
Still not working. I’m so confused. It hides the div, but I’m unsure if its doing anything else.[code][/code]
i tried putting alerts at sections in the code, its not recognizing the submit function I think, so I’m checking the proper syntax and making sure that is okay
Also, why do you have prevent default in there when I have the return false in there assuming that the form error message shows, that means the form shouldn’t be submitted, therefore I put the return false in there when that condition is met. What is the function of that(i know what its for, just wondering why you put it in there when I already have return false in there, i would have assumed that was fine since i have return false at the end of the function)? Just curious
There was a reason I pasted your entire file there, and not just the jQuery.
I did say I added an ID to the form. Please go back to my code/last post and read it / use it.
WHen you submit, the HTML will already make it so the form is submitted / the default form action happens.
The jQuery won’t have time to run / won’t run. You need to prevent the default behavior for the form submission.
Your return false will only return false if the function has the ability to run. WIthout preventDefault(), the form will still submit and your return false won’t do ANYTHING to stop it.