Note that there are some explanatory texts on larger screens.

plurals
  1. POCan I simplify/optimise this Jquery code at all?
    primarykey
    data
    text
    <p>I've wrote some code that will check two dates - they are split into two day inputs (#enddate-1-dd, #date-1-dd), two month inputs (#enddate-1-mm, #date-1-mm) and two year inputs (#enddate-1, #date-1)</p> <p>I wanted to check first of all that they're all actually numbers, but then I wanted to check each one to make sure it's in a date format, at the moment it's like this:</p> <pre><code>function validate_form () { retVal = true; // if the statements below fail, return true if(retVal == true) { // check whether the available hours they've entered are a valid time! $(":text").each(function() { $this = $(this); // cache the object if (isNaN($this.val())) { $this.focus(); $.jGrowl('Please enter a valid date!', { theme: 'smoke' }); retVal = false; return false; } }); } if(retVal == true) { $("#date-1-dd").each(function() { $this = $(this); // cache the object if ($this.val() &gt; 31) { $this.focus(); $.jGrowl('Please enter a valid day, should be no more than 31!', { theme: 'smoke' }); retVal = false; return false; } }); } if(retVal == true) { $("#enddate-1-dd").each(function() { $this = $(this); // cache the object if ($this.val() &gt; 31) { $this.focus(); $.jGrowl('Please enter a valid day, should be no more than 31!', { theme: 'smoke' }); retVal = false; return false; } }); } if(retVal == true) { $("#date-1-mm").each(function() { $this = $(this); // cache the object if ($this.val() &gt; 12) { $this.focus(); $.jGrowl('Please enter a valid month, should be no more than 12!', { theme: 'smoke' }); retVal = false; return false; } }); } if(retVal == true) { $("#enddate-1-mm").each(function() { $this = $(this); // cache the object if ($this.val() &gt; 12) { $this.focus(); $.jGrowl('Please enter a valid month, should be no more than 12!', { theme: 'smoke' }); retVal = false; return false; } }); } if(retVal == true) { $("#date-1").each(function() { $this = $(this); // cache the object if ($this.val() &lt; 1900 || $this.val() &gt; 3000) { $this.focus(); $.jGrowl('Please enter a valid year!', { theme: 'smoke' }); retVal = false; return false; } }); } if(retVal == true) { $("#enddate-1").each(function() { $this = $(this); // cache the object if ($this.val() &lt; 1900 || $this.val() &gt; 3000) { $this.focus(); $.jGrowl('Please enter a valid year!', { theme: 'smoke' }); retVal = false; return false; } }); } return retVal; // return either true or false, depending on what happened up there! ^ </code></pre> <p>}</p> <p>Sorry if it seems like I'm asking a silly question, as my code is working okay, I just think it's a rubbish way of doing it, with loads of repetition, but I can't really think of any way of doing it more efficiently?</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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