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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

cfinput and javascript onfocus 1

Status
Not open for further replies.

kafka

Programmer
Oct 17, 2000
27
0
0
US
Arggh, Still not working.
Is this a limitation of Cold Fusion validation(or my programming skills)? I have been trying for some time to use an JS onfocus call from a cfinput tag
In the cfinput, if a user does not enter a surname, a message will pop up telling the user to enter a valid surname based on my validate tag. cfinput does not hve tag to do an onfocus, so I used onerror to call a JS function which should set the focus on that input. I can get around this by changing the cfinput tag to an input tag then using an onfoucs, but I would like to avoid doing this. I have tried lots of combinations to get this to work but no luck. Anyone have any ideas?

function checkform2() {
document.individual_registration_processing.surname.focus();

}


<cfinput type=&quot;text&quot; name=&quot;surname&quot; size=&quot;20&quot; required=&quot;yes&quot; Message=&quot;Please enter a valid surname&quot; onerror=&quot;return checkform2()&quot;></td>



 
do you **have to** use cfinput instead of input ??
 
Yes

This is becase a lot of my inputs require validation for eurodate or integer. Thus I need a way to mix my cfinput validation with regular JS onfocus
 
The first step is to understand that for validation of cfinput all ColdFusion does is dynamically create its own client side Javascript validation to download to the browser. I always write my own Javascript validation as this gives me more control in the long run.

To acheive your goal: the value you place in the onerror attribute is just the name of the function ie
onerror=&quot;checkform2&quot; no brackets required.

ColdFusion now hands the responsibility for the final stage of validation to your checkform2 function. It will pass a number of objects to your function to help you.

rewrite your function as

function checkform2(form,element,value,text) {
alert(text); //my alert text
element.focus(); //focus the element
return false; //stop the form submission
}

This function will now focus ANY cfinput text box that has the attribute onerror=&quot;checkform2&quot;.
</thrud>
 
I think when you use <cfinput ... > the validate attribute will override the JavaScript validation that you are attempting to use.

A sneaky way around this is to use the validate attribute to validate for eurodate or whatever and then view the page in a browser. Then if you view source, you will see the JavaScript that the CF server has produced to validate this input and build this into your other validation rules that you want by changing the <cfinput...> to <input...> and using the validation onblur of the control, or better still onsubmit of the form, where you can do all of your validation in one go.

Simon
 
Thank you my good friends(Thrud especially as I used your solution). Everything is now kosher

Cheers,


Sahil
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top