custom validation

  • Thread starter Thread starter Mr. SweatyFinger
  • Start date Start date
M

Mr. SweatyFinger

hi.
i have a bunch of controls that get validated with the validation controls
in vs2005

there are two that i could not find a control for so i wrote some
javascript.

so i set my submit button to
OnClientClick="vRealtor()"


yet my function is not executed.
What is the problem??


<script language="javascript" type="text/javascript" >

function vRealtor() {

if (document.all.RealtorTextBox) {

if (document.all.RealtorTextBox.value != "")

{

if (document.all.RealtorCompanyTextBox.value == "")

{

alert("You must enter a company since you entered a realtor name")

}

else

{

alert("asdfasdf")

}

}

else

{

alert("3333")

}



}

}

</script>
 
Howdy,

I think it actually is executed. Before I start explaining reasons I suspect
so, could I ask you why other controls could not be found? Are you using
validation controls in conjunction with template data bound web controls like
grid view, FormView or details view? If yes let me know. I found two
potential issues:

1. You don’t prevent postback returning true/false from the validation
function
2. You used control.ID instead of ClientID

Fully working example:


<asp:TextBox ID="RealtorTextBox" runat="server"/>
<asp:TextBox ID="RealtorCompanyTextBox" runat="server"/>
<asp:Button ID="Button1" runat="server" Text="Button"
OnClientClick="return vRealtor();" />

<script type="text/javascript">


function trim( value )
{
value = value.replace(/((\s*\S+)*)\s*/, "$1");
return value.replace(/\s*((\S+\s*)*)/, "$1");

}

function vRealtor()
{
var realtorTextBox = document.getElementById('<%=RealtorTextBox.ClientID
%>');

if (trim(realtorTextBox.value) != '')
{
var realtorCompanyTextBox =
document.getElementById('<%=RealtorCompanyTextBox.ClientID %>');
if (trim(realtorCompanyTextBox.value) == '')
{
alert("You must enter a company since you entered a realtor name");
return false;
}
}
return true;
}

</script>
 
hi.
i have a bunch of controls that get validated with the validation controls
in vs2005

there are two that i could not find a control for so i wrote some
javascript.

so i set my submit button to
OnClientClick="vRealtor()"

yet my function is not executed.
What is the problem??

<script language="javascript" type="text/javascript" >

function vRealtor() {

if (document.all.RealtorTextBox) {

if (document.all.RealtorTextBox.value != "")

{

if (document.all.RealtorCompanyTextBox.value == "")

{

alert("You must enter a company since you entered a realtor name")

}

else

{

alert("asdfasdf")

}
}

else

{

alert("3333")

}
}
}

</script>


Have you tried using a custom validator?

You can tell it which control to validate.
You just set your function to the ClientValidationFunction

Use the source and arguments variables (which are automtically passed
in be the server side code) to test the data and tell the control
whether it is valid or not. Here is an example that checks for an
upper and lower limit (this also sets the backcolor and forecolor of
the control, so you can ignore that bit).

<script language='javascript'> function ClientValidatestr004(source,
arguments) {
if(arguments.Value < 3) { arguments.IsValid=false;
document.form1.str004.style.backgroundColor = 'Blue';
document.form1.str004.style.color = 'white'; }

else if(arguments.Value > 4){ arguments.IsValid=false;
document.form1.str004.style.backgroundColor = 'Red';
document.form1.str004.style.color = 'white'; }

else { arguments.IsValid=true;
document.form1.str004.style.backgroundColor = '#FFFF80';
document.form1.str004.style.color = 'black'; } } </script>

Hope this is helpful

Jared
 
you should use the custom validator and supply the client script.

-- bruce (sqlwork.com)
 
Hi when I said "there are two that I could not find a control for"

i meant
there are two controls i could not find avalitation control for.
does that help? >>
 
Back
Top