Note that there are some explanatory texts on larger screens.

plurals
  1. POString Time Validations (In javascript)
    primarykey
    data
    text
    <p>I have a text box which accepts time(max of 5 characters only), and a drop down which accepts am or pm value. </p> <p>I need to perform some validations for the string values entered into the text box such as:</p> <ol> <li>If user enters 9 => Should be changed to 0900</li> <li>9:3 => 0930</li> <li>09:3 => 0930</li> <li>93 => alert ('Invalid Hours value)</li> <li>115 => 0115</li> <li>12 => 1200</li> <li>Invalid entries such as !@#$%^&amp;*()&lt;>?/~`,;'"[]_-abcdefg.. => alert ('Invalid Time Value') should be displayed.</li> </ol> <p>So far, all I've achieved is replacing the : with ''. For example, if user enters 09:00 => 0900</p> <p>I need something like:</p> <p>if clks is 1 digit, then rightpad clks with 2 zeroes.</p> <p>if 2 digits: clks > 12 , then alert(Invalid hours value)</p> <p>if 3 digits: clks &lt; (%59) (i.e checking last 2 chars) , then leftpad with 1 zero or clks > (%59) , then alert ('Invalid minutes value)</p> <p>if 4 digits: clks>12% (checking first 2 chars), alert ('invalid hours value') or (clks>12% and clks>59%) , then alert('invalid clock time') or (clks&lt;12%) and (clks&lt;59%) , then accept the number as it is.</p> <p>if 5 digits: (and all are numbers), then alert('invalid clk time')</p> <p>These validations need to be done within script tag. (The backend language that I've used is jsp.)</p> <p>Pls help me :(</p> <p>Here is a part of the code that I have written:</p> <pre><code>&lt;script type='text/javascript'&gt; function clocks(){ var clk = document.getElementById('TIME').value; var clks = clk.replace('/:/g',''); var ampm = document.getElementById('AMPM').value; var add = 1200; if (clks=="") { alert("You must enter clock time"); } else { if (ampm=='p') { clks=parseFloat(clks) + parseFloat(add); } } } .... &lt;/script&gt; </code></pre>
    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.
 

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