jQuery form validation - 'Submit()' problem -


I am trying to learn how to validate a form and I have this problem:

When I run this code and I do not enter anything - the form should not be sent, but it does. My question is ... Why and how can I fix it?

html:

  & lt; Form method = "post" action = "#" & gt; & Lt; Fieldset & gt; & Lt; Story & gt; Formular & lt; / Narrative & gt; & Lt; Label = "name" & gt; Name: & lt; / Label & gt; & Lt; Input type = "text" class = "required" name = "name" value = "" /> & Lt; Br / & gt; & Lt; Br / & gt; & Lt; = "Pass" for label & gt; Password: & lt; / Labels & gt; & Lt; Input type = "password" class = "required" name = "pass" value = "" /> & Lt; Br / & gt; & Lt; Br / & gt; & Lt; Input type = "submit" value = "submit!" / & Gt; & Lt; / Fieldset & gt; & Lt; / Form & gt;  

the jQuery:

  $ ('# obal form'). Submit (function () {$ ('. Required'). ((((($ (This ($) value .val () == "") {if (errorCount === undefined) {var errorCount = 1 ;} Else {++ errorCount;}}}); (error_group; 0) {window.alert ('you did not pass'); return;} and {back true;}});  

If you have any further improvements that can take codes, then you can also tell me, thank you ..: -)

Your current code is not working because errorCount is local for anonymous operation by each method . This will not be visible outside

This means that when you type errorCount & gt; 0 After each of your methods, it evaluates the incorrect and submits the form.

I would like to write it to my function,

  $ ('# obal form'). Define your error. If you define zero variables for each zeros before iterating by each, errorCount will be available in each frequency function. $ ('Required') . (Function () {// Due to Javascript Closer, you can get the error code in this function. If ($ (this) .val () === '') Error: ++;}); // If there is an error, then we come back. The browser will continue to submit the form to the browser, if (error group == 0) returns; // If the control comes, it means that there were errors. 'Validation failure.');});  

Edit:

Why does your code not work?

Let's name the name of the anonymous function (my idol). 'Ech' method has been passed.

  $ ('# obal form'). Submit (function () {$ ('required.') Each (myEachLoop function) {If ($ (this) .val () == "") {if (errorCount === undefined) {var errorCount = 1 ; // You define the error that is 'local' for the 'myEachLoop' function. Once myEachLoop function is over. ErrorCount variable has gone!} And {++ errorCount;}}}); // Complicated 'undefined' is here because the previously defined defaced variable variable is gone. If (errorCount> gt; 0) {windows .lert ('you did not pass'); Returning;} Other {Return True;}});  

Now you see a mistake in your code?

You also need to know why my code is working.


Comments

Popular posts from this blog

c# - ListView onScroll event -

PHP - get image from byte array -

Linux Terminal Problem with Non-Canonical Terminal I/O app -