Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript validation not performing as exepected
    primarykey
    data
    text
    <p>I have a problem with my JS validation code. When I submit the form and there are errors, the form shouldn't go any further. But yet, the code doesn't stop, instead it carries on to the next line, which shows a successful message although there are still errors.</p> <p>And I've clearly written that if, for example the field is empty, then <code>return false</code>...</p> <p>Why does the code carry on to the next line, even though there's <code>return false</code>?</p> <p>Press <code>submit</code> and you'll see what I mean.</p> <p>JS:</p> <pre><code>(function(window, $) { var Namespace = (function(Namespace) { Namespace = { // Main run : function() { this.validate.run('form'); }, // Validation validate : { // error message span messageBox : '&lt;span class="message" /&gt;', // add any field here fields : { nameField : $('#contact-name'), emailField : $('#contact-email'), phoneField : $('#contact-phone') }, // run validation run : function(formName) { $(formName).on('submit', $.proxy(this.validateField, this)); }, validateField : function() { for (var field in this.fields) { if (this.fields.hasOwnProperty(field)) { this.checkField(this.fields[field]); } } $('#general-message-section').text('Form successfully sent, thank you!'); return false; }, checkField : function(field) { var messageBox = $(this.messageBox); field.closest('li').find('.message').remove(); if (field.hasClass('required')) { if (!field.val()) { messageBox.text('This field is empty!'); field.closest('li').append(messageBox); return false; } } if (this.fields.emailField.val()) { this.fields.emailField.closest('li').find('.message').remove(); if (!this.fields.emailField.val().match(this.regEx.email)) { messageBox.text('Only email format accepted!'); this.fields.emailField.closest('li').append(messageBox); return false; } } if (this.fields.phoneField.val()) { this.fields.phoneField.closest('li').find('.message').remove(); if (!this.fields.phoneField.val().match(this.regEx.numbers)) { messageBox.text('Only numbers are accepted!'); this.fields.phoneField.closest('li').append(messageBox); return false; } } }, regEx : { email : /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/, numbers : /^[0-9]+$/ } } }; return Namespace; }(Namespace || {})); // make global window.Namespace = Namespace; }(window, jQuery)); // run it... Namespace.run(); </code></pre> <p>HTML:</p> <pre><code>&lt;p id="general-message-section"&gt;&lt;/p&gt; &lt;form id="contact-form" method="post" action="#"&gt; &lt;fieldset&gt; &lt;ul&gt; &lt;li&gt; &lt;label for="contact-name"&gt;Contact name *:&lt;/label&gt; &lt;input type="text" id="contact-name" tabindex="1" class="required" autofocus /&gt; &lt;/li&gt; &lt;li&gt; &lt;label for="contact-email"&gt;Contact email address *:&lt;/label&gt; &lt;input type="text" id="contact-email" tabindex="2" class="required" /&gt; &lt;/li&gt; &lt;li&gt; &lt;label for="contact-phone"&gt;Contact phone number:&lt;/label&gt; &lt;input type="text" id="contact-phone" tabindex="3" /&gt; &lt;/li&gt; &lt;li&gt; &lt;input type="submit" id="submit-btn" tabindex="4" value="Submit" /&gt; &lt;/li&gt; &lt;/ul&gt; &lt;/fieldset&gt; &lt;/form&gt; </code></pre> <p>Many thanks</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload