Note that there are some explanatory texts on larger screens.

plurals
  1. POUse JavaScript to allow only specific characters in HTML input
    primarykey
    data
    text
    <p>I have written some JavaScript and jQuery code that accepts only numeric input in a textbox. But this is not enough; I need to limit the input to certain numbers.</p> <p>This textbox needs to deal with SSN numbers (Swedish SSN), and it has to start with 19 or 20. I want to force it to start with these numbers, but I can't manage to limit it to these.</p> <pre><code> $('input.SSNTB').keydown(function (event) { var maxNrOfChars = 12; var ssnNr = $('input.SSNTB').val(); if (event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 27 || // Allow: Ctrl+A (event.keyCode == 65 &amp;&amp; event.ctrlKey === true) || // Allow: home, end, left, right (event.keyCode &gt;= 35 &amp;&amp; event.keyCode &lt;= 39)) { // let it happen, don't do anything return; } else { // Ensure that it is a number and stop the keypress if (((event.keyCode &lt; 48 || event.keyCode &gt; 57) &amp;&amp; (event.keyCode &lt; 96 || event.keyCode &gt; 105))) { console.log("if-1"); event.preventDefault(); } if (event.shiftKey == true) { console.log("if-3"); event.preventDefault(); } //rules to make sure the textbox starts with correct number if (event.keyCode != 49 || event.keyCode != 50 || event.keyCode != 97 || event.keyCode != 98) { console.log("if-4, Keycode:" + event.keyCode); event.preventDefault(); } } }); </code></pre> <p>The last if-case is executed to for testing this it is. It is executed as planed but it wont limit the input chars as its built for.</p> <p>any tips or ideas?</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.
 

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