JavaScript - Validar input cuando se pulsa enter

 
Vista:

Validar input cuando se pulsa enter

Publicado por Oscar Medina (8 intervenciones) el 15/10/2001 19:54:55
Tengo un problema al querer validar un input type="text", tengo este codigo (resumido):

function checkInformation(forma) {
var bError = false;
if (isNaN(forma.txtNip.value)) {
alert("Nip debe de ser numerico.");
forma.txtNip.focus();
bError = true;
return;
}
if (!bError) {
forma.submit();
}
return;
}

...

<form name="frmPreSol" method="post" action="movperi2.asp">
NIP:<input name="txtNip" type="password">
<a href="javascript:checkInformation(document.frmPreSol)"> Procesar</a>
</form>

mi problema es que no deseo que en ese campo se introduzcan caracteres, si le doy ckick al link Procesar la validacion toma efecto y me indica que el Nip debe ser numerico, mas sin embargo, si me pocisiono en el campo, tecleo el Nip y enseguida pulso enter el codigo de javascript no llama la funcion checkInformation() y por lo tanto no ejecuta ninguna validacion

espero me puedan ayudar
de antemano gracias

Lic. Oscar Medina
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

espero sirva esto

Publicado por juan arturo (6 intervenciones) el 15/10/2001 20:33:07
lo tome de aqui: www.javascript.com seccion "forms":

A form can be submitted via the Enter key when it consists of only one text field, and the cursor is currently located in that field. Sometimes, we want to force an action by the Enter key, without submitting the form. The trick is to use the onSubmit event handler, do the required action when the trigger goes off, and then cancel the event by returning a false value. In this way, we still utilize the advantages of the submit button (submission via the Enter key) without actually submitting the form.
Let's take an example. Suppose a form's header is defined as:

<FORM NAME="organizer", onSubmit="display(); return false;">
and is composed of two elements:

A text field, NAME="prefix"
A submit button, VALUE="Display"

The user can submit the form by clicking the button labeled "Display," or by pressing Enter when the cursor is located in the text field. When the user submits the form, the onSubmit event handler is triggered. It invokes the display() function.
The onSubmit event handler returns false in order to cancel the form's submission. If we aren't really submitting the form, then why didn't we use a standard button instead of a submit button? The answer is simple. If we used a standard button to invoke display() via its onClick event handler, that would be the only way to call display(). By using the form's onSubmit event handler to call the function, the user can also load the desired page by pressing Enter within the text field.

Learn more about canceling a form submission in Column 15
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar