customvalidator clientsidescript?

G

Guest

I'm trying to use a custom validator but it seems to trigger no matter what I
do. I'm not sure i'm referencing the controls involved correctly from the js.
My FormView is named fvRegDetail. I've tried both just the control name and
the formview name appended with the control name as I find it in the rendered
source.

function ClientValidateCommuteMethod(ctl, args)
{

var txt1 = document.getElementById ("fvRegDetail_tbDaysDriveAlone").value;
var txt2 = document.getElementById ("fvRegDetail_tbDaysVanPool").value;
var txt3 = document.getElementById ("fvRegDetail_tbDaysMARTABus").value;
var txt4 = document.getElementById ("fvRegDetail_tbDaysBicycle").value;
var txt5 = document.getElementById ("fvRegDetail_tbDaysTelework").value;
var txt6 = document.getElementById ("fvRegDetail_tbDaysCarpool").value;
var txt7 = document.getElementById ("fvRegDetail_tbDaysCCTBus").value;
var txt8 = document.getElementById ("fvRegDetail_tbMARTARail").value;
var txt9 = document.getElementById ("fvRegDetail_tbDaysWalk").value;

if (txt1.length > 0 ||
txt2.length > 0 ||
txt3.length > 0 ||
txt4.length > 0 ||
txt5.length > 0 ||
txt6.length > 0 ||
txt7.length > 0 ||
txt8.length > 0 ||
txt9.length > 0 ){
args.IsValid = true;
}else{
args.IsValid = false;
}
return args.IsValid;
}

My Custom Validator:
<asp:CustomValidator ID="CustomValidator1" runat="server"
ClientValidationFunction="ClientValidateCommuteMethod"
ErrorMessage="Indicate a current commute method"
ValidationGroup="regForm"
OnServerValidate="CustomValidatorCommuteMethod_ServerValidate" >
Please indicate commute method - 1-7 days a week.
</asp:CustomValidator><br />

Thanks for any suggestions on this.
 
G

Guest

Send the clientIDs of the controls that you need to manipulate with
JavaScript like this:
void Page_PreRender(object sender, EventArgs e)
{
// Get a ClientScriptManager reference from the Page class.
ClientScriptManager cs = Page.ClientScript;
Type cstype = this.GetType();

// Check to see if the startup script is already registered.
if (!cs.IsStartupScriptRegistered(cstype, "InitVarsForJavascript"))
{
String cstext = "var control1ID='" + tbDaysDriveAlone.ClientID +
"';";
cs.RegisterStartupScript(cstype, "InitVarsForJavascript",
cstext, true);
}
}

Then in your JavaScript you would write:
var txt1 = document.getElementById (control1ID).value;
 
O

Onwuka Emeka

If you are not dynamically adding controls to the page you might also want
to do this inline
var txt1 = document.getElementById ('<%=tbDaysDriveAlone.ClientID%>').value;
etc

place the javascript in the same template as the one that contains the
controls
 
G

Guest

Thanks Phillip, this takes me to new heights of expertise, the air up here
seems pretty thin ;)
 
G

Guest

Onwuka

This didn't work for me, maybe I'm missing something,

Compiler Error Message: CS0103: The name 'tbDaysDriveAlone' does not exist
in the current context
Source Error:

Line 545:function ClientValidateCommuteMethod(ctl, args)
Line 546:{
Line 547: var txt1 = document.getElementById
('<%=tbDaysDriveAlone.ClientID%>').value;
Line 548: var txt2 = document.getElementById
('<%=tbDaysVanPool.ClientID%>').value;
Line 549: var txt3 = document.getElementById
('<%=tbDaysMARTABus.ClientID%>').value;



I placed the code inside my InsertItemTemplate:
<script type="text/javascript">
function ClientValidateCommuteMethod(ctl, args)
{
var txt1 =
document.getElementById('<%=tbDaysDriveAlone.ClientID%>').value;
var txt2 = document.getElementById('<%=tbDaysVanPool.ClientID%>').value;
var txt3 = document.getElementById('<%=tbDaysMARTABus.ClientID%>').value;
var txt4 = document.getElementById('<%=tbDaysBicycle.ClientID%>').value;
var txt5 = document.getElementById('<%=tbDaysTelework.ClientID%>').value;
var txt6 = document.getElementById('<%=tbDaysCarpool.ClientID%>').value;
var txt7 = document.getElementById('<%=tbDaysCCTBus.ClientID%>').value;
var txt8 = document.getElementById('<%=tbDaysMARTARail.ClientID%>').value;
var txt9 = document.getElementById ('<%=tbDaysWalk.ClientID%>').value;

if (txt1.length > 0 ||
txt2.length > 0 ||
txt3.length > 0 ||
txt4.length > 0 ||
txt5.length > 0 ||
txt6.length > 0 ||
txt7.length > 0 ||
txt8.length > 0 ||
txt9.length > 0 ){
args.IsValid = true;
}else{
args.IsValid = false;
}
return args.IsValid;
}
</script>


Would be nice if this method worked, as its simple and clean. Note the
controls in question are inside a Panel in the InsertItemTemplate
 
G

Guest

This worked great! I had to use FindControl as in:

String cstext = "var DaysDriveAloneID='" +
fvRegDetail.FindControl("tbDaysDriveAlone").ClientID + "';";

Thanks, finally I can move on to release this page!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top