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

Using the Custom Validator

Status
Not open for further replies.

lunargirrl

Programmer
Jan 26, 2004
77
BR
I am trying to validate a textbox in my page. It cant contains less than 7 characters.
This is what I am doing:

<head>
<script language="JavaScript">
function PassLength( oSrc, txtPass ){ var retValue = true; if(txtPass.length > 7){ retValue = false;} return retValue } </script>
</head>

In my control:
Display = Dynamic
Error Message = You must type more than 7 characters.
ClientValidationFunction = PassLength
ControltoValidate = txtPass

It doesnt work, I was searching more in the net and found in some sites 2 functions, one for the server and one for the client? I didnt understand very well. Could someone give me tips and help about this subject?

Thanks
Gis.
 
//Not less then 7 characters
function ValidLength( source, arguments ){
if( arguments.Value.length < 7 )
arguments.IsValid = false;
else
arguments.IsValid = true;
}

this should work. if you get errors try changing the capitalization of:
Value -> value
-or-
length -> Length

Jason Meckley
Database Analyst
WITF
 
Jason, I added your function inside of my head tag in my aspx (html part), only this function is there, and connected it to my custom control. But it doesnt work. Should I add something else?

tia,
gisele
 
is it erroring out or not firing?
check to make sure the button/link you click to validate has [tt]CausesValidation = true[/tt]

you may also want to try:
if( len(arguments.Value) < 7 )
I don't think this will work, but it's worth a shot.

If your still having issues, post your HTML and i'll take a look at it.

Jason Meckley
Database Analyst
WITF
 
hello Jason, I think its not firing.The second option that you wrote didnt work as well.
I am going to post my html code here, i think its better for you to check :)

<HTML>
<HEAD>
<title>New User</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content=" name="vs_targetSchema">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="JavaScript">
function TamanhoSenha( oSrc, txtSenha ){    
if( txtSenha.value.Length < 7 )
txtSenha.IsValid = false;    
else        
txtSenha.IsValid = true;
}
</script>
</HEAD><body leftMargin="0" topMargin="0" MS_POSITIONING="GridLayout" marginheight="0" marginwidth="0">

<table height="100%" cellSpacing="0" cellPadding="0" width="781" border="0">
<TBODY>
<tr>
<td style="HEIGHT: 417px" bgColor="#eeeeee"><asp:image id="Image2" style="Z-INDEX: 101; LEFT: 0px; POSITION: absolute; TOP: 0px; Design_Time_Lock: True"
runat="server" Design_Time_Lock="True" ImageUrl="header.gif"></asp:image>
<form id="Form1" method="post" runat="server">
<asp:panel id="pnlControles" style="Z-INDEX: 103; LEFT: 168px; POSITION: absolute; TOP: 144px"
Height="456px" Width="552px" runat="server">
<asp:CompareValidator id="CompareValidator1" style="Z-INDEX: 107; LEFT: 24px; POSITION: absolute; TOP: 368px"
ForeColor="#C00000" Font-Names="Arial" Font-Size="8pt" Width="250px" runat="server" ControlToValidate="txtRepetir"
ControlToCompare="txtSenha" ErrorMessage="CompareValidator" Font-Bold="True">Sua senha não foi repetida corretamente!</asp:CompareValidator>
<asp:imagebutton id="cmdNovo" style="Z-INDEX: 127; LEFT: 336px; POSITION: absolute; TOP: 376px" runat="server"

... - a lot of code - buttons, labels...
<asp:textbox id="txtRepetir" style="Z-INDEX: 120; LEFT: 168px; POSITION: absolute; TOP: 304px"
tabIndex="1" ForeColor="Black" Height="22px" Width="193px" runat="server" MaxLength="10"
BackColor="Ivory" BorderWidth="1px" BorderStyle="Solid" BorderColor="Black" TextMode="Password"></asp:textbox>
<asp:label id="lblrepetir" style="Z-INDEX: 121; LEFT: 24px; POSITION: absolute; TOP: 312px"
Font-Names="Arial" Font-Size="X-Small" Height="16px" Width="104px" runat="server" Font-Bold="True">Repita a Senha:</asp:label>

<asp:textbox id="txtSenha" style="Z-INDEX: 124; LEFT: 168px; POSITION: absolute; TOP: 272px"
tabIndex="1" ForeColor="Black" Height="22px" Width="194px" runat="server" MaxLength="10"
BackColor="Ivory" BorderWidth="1px" BorderStyle="Solid" BorderColor="Black" TextMode="Password"></asp:textbox>
<asp:Label id="lblTexto" style="Z-INDEX: 125; LEFT: 24px; POSITION: absolute; TOP: 384px" ForeColor="#C00000"
Font-Names="Arial" Font-Size="8pt" Height="16px" Width="328px" runat="server" Font-Bold="True">Label</asp:Label>
<asp:label id="lblSenha" style="Z-INDEX: 126; LEFT: 24px; POSITION: absolute; TOP: 280px" Font-Names="Arial"
Font-Size="X-Small" Height="16px" Width="64px" runat="server" Font-Bold="True">Senha:</asp:label>

<asp:CustomValidator id="CustomValidator1" style="Z-INDEX: 105; LEFT: -96px; POSITION: absolute; TOP: 168px"
Height="32px" Width="112px" runat="server" ControlToValidate="txtSenha" ErrorMessage="A senha deve conter entre 7 e 10 caracteres."
ClientValidationFunction="TamanhoSenha"></asp:CustomValidator>
</asp:panel></form>
...
</TBODY>
</table>
</body>
</HTML>

Thank you,
Gis.
 
try changing the name of the values you pass to the function. I pretty sure they have to be 'source', and 'agruments'.

also make sure to wrap your entire sever side code for the custom validator in
try
agrs.isvalid = len(agrs.value) > 7
Catch
agrs.isvalid = false
end try



Jason Meckley
Database Analyst
WITF
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top