validate asp.net text field?

-1

I have an asp text field: textbox in which I must enter 1 to 2 whole numbers from a keyboard created with button type="button", I have not used asp: button because this keyboard and this textbox are inside a modal of bootstrap, and if I used an asp: button I just rebooted the page and closed the modal. Well the case esque I need to validate the textbox so that it only accepts a certain amount of numbers.

I tried with the validation tools that .net already has but it does not work for me, it does not let me write anything in the textbox with the button, 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 anything.

I hope I made myself understood. Thanks 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;
		
	}
    
asked by sunflower 06.04.2018 в 20:48
source

1 answer

0

If you could show the code it would be more helpful. What I see is that in your function validate the id of the element may not correspond to the id of the TextBox. Webforms have their particular way of generating IDs.

You can try this in your function to get the actual id of the textbox in HTML:

 sem=$('#' + '<%= sem.ClientID %>').val();
 // sem.ClientID hace referencia al id del textbox

You can also do it this way:

 sem=$('[id$=sem]').val();
 // [id$=sem] igualmente sem hace referencia al id declarado en el textbox, básicamente lo que hace ese selector es buscar los elementos que en el id terminen con la palabra sem

link

    
answered by 06.04.2018 / 23:09
source