VALIDAR
SOLO NÚMEROS-LETRAS EN TEXTBOX EN GRIDVIEW (ITEMTAMPLATE)
function validanumero(e) {
tecla = (document.all) ? e.keyCode :
e.which;
if
(tecla == 8) return true;
patron = /\d/;
return patron.test(String.fromCharCode(tecla));
}
function validaletra(e) {
tecla = (document.all) ? e.keyCode :
e.which;
if
(tecla == 8) return true;
patron = /[A-Za-z\s]/;
te =
String.fromCharCode(tecla);
return patron.test(te);
}
2.
Hacemos la llamada al Archivo
js.
<script src="../../JavaScript/ValidateNumberLetters.js"
type="text/javascript"></script>
3.
Instanciamos al control en el
Page_PreRender de la página y llamamos a la función que validará:
protected void Page_PreRender(object
sender, EventArgs e)
{
TextBox txtQuantity;
foreach (GridViewRow
gr in gridItems.Rows)
{
if (gr.RowType == DataControlRowType.DataRow)
{
txtQuantity = (TextBox)gr.FindControl("txtQuantity");
//Solo
permitir números
txtQuantity.Attributes.Add("onKeypress", "javascript:return
validanumero(event);");
//Solo permitir letras
//txtQuantity.Attributes.Add("onKeypress",
" javascript:return
validaletra(event);");
}
}
}
No hay comentarios:
Publicar un comentario