I have a text field asp:textbox which I must enter from 1 to 2 integers from a keyboard created with button type="button", I have not used asp:button because this keyboard and this textbox are inside a bootstrap modal , and if I used an asp:button it just restarted the page and closed the modal. Well the case is that I need to validate the textbox so that it only accepts a certain number of numbers.
I tried it with the validation tools that .net already has but it doesn't work for me, it doesn't let me write anything in the textbox with the buttons, so I removed it and tried it with jquery, but it only limits the space when I write with the keyboard of my pc, and if I write with the keyboard that I did, it does not limit me at all.
I hope I made myself understood. Thank you for reading.
<asp:textbox id="sem" runat="server" autocomplete="off"></asp:textbox>
<button id="b1" type="button">1</button>
<button id="b2" type="button">2</button>
<button id="b3" type="button">3</button>
<button id="b4" type="button">4</button>
<button id="b5" type="button">5</button>
<button id="b6" type="button" ...etc..
//Utilice esta función pero como ya dije, si escribo con el teclado que he
hecho me deja escribir más de 2 números.
function validar(){
sem=$('#head_sem').val();
if (sem.length<2){
return true;
}
else {
alert('Maximo 2 caracteres');
return false;
}
If you could show the code it would be more helpful. What I see is that in your validate function the id of the element may not correspond to the id of the TextBox. Webforms have their own way of generating IDs.
You can try this in your function to get the actual id of the HTML textbox:
You can also do it this way:
https://api.jquery.com/attribute-ends-with-selector/#attributevalue