Jump to content

How can I use focus() ?


growth

Recommended Posts

I am using the code below, but the command focus() doesn´t work. Why?PS: the form name and the field name are corrects.function ValidaCEP(field) {if (field.lenght != 8){alert ("O CEP deve conter 8 dígitos!");document.AlteraClienteForm.field.focus(); return false;}return true;}Thank you.

Link to comment
Share on other sites

You are targeting by using form and form elements names, besides, I guess you cannot replace one of those names by the passed variable.Use this:

function ValidaCEP(field) {if (field.length != 8){alert ("O CEP deve conter 8 dígitos!");document.forms['AlteraClienteForm'].elements[field].focus(); return false;}return true;}
Note the "length" property was misspelled, and elements[field] is not quoted - afterall, here you do may use the variable :)
Link to comment
Share on other sites

You are targeting by using form and form elements names, besides, I guess you cannot replace one of those names by the passed variable.Use this:Note the "length" property was misspelled, and elements[field] is not quoted - afterall, here you do may use the variable :)

Thank you, DanI did like your sugestion, but it didn´t work. The focus is going to next field.The javascript code :function ValidaCEP(field) { if (field.length != 8) { alert ("O CEP deve conter 8 dígitos!"); document.forms['ClientesForm'].elements[field].focus(); return false; } return true;}The part of HTML code :<TR> <TD align="right" valign="middle"><DIV CLASS="normal">CEP:</DIV></TD> <TD align="left" valign="middle"><input type="text" class="" name="<%= Defines.PARAM_CEP_CLIENTE%>" value="" maxlength="8" size="9" onChange="ValidaCEP(<%= Defines.PARAM_CEP_CLIENTE %>.value)"> <font size="1"> (Digite o CEP sem o traço)</font> </TD></TR>
Link to comment
Share on other sites

You are using ASP aren't you?Eitherway, you may replace the name of the onChange attribute with the onmousedown event. The change event doesn't always work like you want (have I experienced).

<TR><TD align="right" valign="middle"><DIV CLASS="normal">CEP:</DIV></TD><TD align="left" valign="middle"><input type="text" class="" name="<%= Defines.PARAM_CEP_CLIENTE%>" value="" maxlength="8" size="9" onmousedown="ValidaCEP(<%= Defines.PARAM_CEP_CLIENTE %>.value)"><font size="1"> (Digite o CEP sem o traço)</font> </TD></TR>
Or when you want to have the function called after the mouse button goes up, use onmouseup :)[*Edit:]This may also not work, it depends on what exactly you want, I don't know your language nor I understand the ASP instructions :) Edited by Dan The Prof
Link to comment
Share on other sites

You are using ASP aren't you?Eitherway, you may replace the name of the onChange attribute with the onmousedown event. The change event doesn't always work like you want (have I experienced).Or when you want to have the function called after the mouse button goes up, use onmouseup :)[*Edit:]This may also not work, it depends on what exactly you want, I don't know your language nor I understand the ASP instructions :)

To validate an input field i would use onblur, that way when the user tries to leave that input box it will validate it, then focus() the input using document.getElementById()You would need to add an id along with name: id="<%= Defines.PARAM_CEP_CLIENTE%>" Something like this
<head><script>function ValidaCEP(identification,field) {if (field.length != 8){alert ("O CEP deve conter 8 dígitos!");document.getElementById(identification).focus(); return false;}return true;}</script></head><body><table><TR><TD align="right" valign="middle"><DIV CLASS="normal">CEP:</DIV></TD><TD align="left" valign="middle"><input type="text" class="" name"part1" id="part11" value="" maxlength="8" size="9" onblur="ValidaCEP(this.id,this.value)"><font size="1"> (Digite o CEP sem o traço)</font> </TD></TR></table><body>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...