Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validating a Field

Status
Not open for further replies.

wf

Programmer
Feb 1, 2001
32
US
How can I validate a field when it lost it's focus?

I used the onblur event but it doesn't work or I don't know how to use it.

Thanks.
 
can you show us a piece of your attempts..? br
Gerard
 
This is my code.

Sub txtFechaVencimiento_onblur()
If len(thisForm.txtFechaVencimiento.value)<> 10 and len(thisForm.txtFechaEfectividad.value)= 10 then
msgbox &quot;El largo del campo 'Fecha de Vencimiento' debe ser de diez caracteres&quot;,,&quot;Cálculo de Prima de Single Interest&quot;

thisForm.txtFechaVencimiento.focus()
exit sub

elseif len(thisForm.txtFechaVencimiento.value)= 10 then
thisForm.txtTermino.value=round(datediff (&quot;m&quot;,thisForm.txtFechaEfectividad.value,thisForm.txt FechaVencimiento.value) + (1/31))

thisForm.txtUnpaid.focus()
End if
End Sub
 
I am not sure that onblur works in Netscape anyway.

You should always include document. before the form reference - again because of Netscape.

If len(document.thisForm.txtFechaVencimiento.value)...

Finally, you can perform validation when the form is submitted - either in the

FORM ... onsubmit= (return false to stop the submit)

or (if you use the Script Object Model) in the

function thisPage_onbeforeserverevent(i_strObject, i_strEvent) {

- you will need to add the i_strObject, i_strEvent parameters yourself. These will be filled with the name of the object and the event (eg 'pbSave' and 'onclick')
To prevent the form from submitting, you need to say

thisPage.cancelEvent = true;

In either of these methods, you can just call your _onblur function if you wish.
(Content Management)
 
Hi,

I found a great article within Microsoft's knoweledge base regarding validation. See Q270714. I found is really helpful... I hope it helps you.

Computergeek
 
If you use a normal textbox, you simply add the onblur attribute:
<input type=text name=txtFred onblur=&quot;txtFred_onblur();&quot;>

But if you have a (server-side) textbox DTC, then you have to 'advise' the DTC to get it to respond to certain events:

txtFred.advise &quot;onblur&quot;, &quot;dummy()&quot;

If I had a server-side function/sub called dummy(), then it would be called every time the txtFred textbox DTC lost its focus - not so helpful!

However, we can trap this server-event on the client, and prevent the server round-trip. You do this in the thisPage_onbeforeserverevent function (please add a PageObject DTC to you page first). The details of this were shown in my previous response.

Note: The 'advise' method of a DTC allows you to add ANY event signature to a form control. When the DTC generates the HTML for the control at runtime, it adds the 'advise' items to the HTML tag:

<INPUT type=&quot;text&quot; id=&quot;txtFred&quot; name=&quot;txtFred&quot; size=3 maxLength=7 value=&quot;0&quot; class=&quot;clsTextBox&quot; onblur=&quot;thisPage._fireEvent('txtFred','onblur'); return false;&quot;>

The _fireEvent bit causes a few things to occur, including the calling of the onbeforeserverevent - where you can call your routines.


If this DTC method seems horrid, well it is! However, it is really quite easy to modify the DTC code to make things much easier. Just edit the _ScriptLibrary\TEXTBOX.ASP and add a new property, called onblur, and in the display routine (near the bottom) output the onblur attribute if the onblur property is not blank. I can give you the full details if you wish. (Content Management)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top