c# - ASP.NET - Validating a CustomValidator -



c# - ASP.NET - Validating a CustomValidator -

i'm trying customvalidator phone call server method (it's checksum, think it's right you'll see can't tell) it's never called, can come in text box , error message never appears, , adding break-point in method reveals it's never called @ not matter what's entered in text box. need client-side validation too?

my onservervalidate looks calling method correctly eyes - @ loss here. help much appreciated!

the asp.net code:

<asp:label id="lblppsn" runat="server" text="pps number: "></asp:label> <asp:textbox id="tbxppsn" runat="server"></asp:textbox> <asp:customvalidator id="customval_ppsn" runat="server" errormessage="please come in valid ppsn" controltovalidate="tbxppsn" onservervalidate="customval_ppsn_servervalidate" forecolor="red"></asp:customvalidator>

the c# code-behind:

protected void customval_ppsn_servervalidate(object source, servervalidateeventargs args) { string checkchar = "", formatchar = "", input = tbxppsn.text; bool newformat = false; if (input.length > 9) { args.isvalid = false; return; } if (input.length == 9) { newformat = true; } char[] ppsarray = input.tochararray(); array.reverse(ppsarray); checkchar = ppsarray[0].tostring(); if (newformat) { checkchar = ppsarray[1].tostring(); formatchar = ppsarray[0].tostring(); } checkchar = checkchar.tolower(); formatchar = formatchar.tolower(); int seed = 2, ppsparsed = 0, ppssum = 0, ppsmod = 0; foreach (char ppschar in ppsarray) { if (int.tryparse(ppschar.tostring(), out ppsparsed)) { ppssum += ppsparsed * seed; seed++; } } if (newformat) { ppssum += convert.toint32(formatchar.tochararray()[0] - 96) * 9; } ppsmod = ppssum % 23; if (ppsmod == 0) ppsmod = 23; if (convert.tostring((char) (96 + ppsmod)).tolower() == checkchar) { args.isvalid = true; return; } else { args.isvalid = false; } }

if never post server server side validation not called. may want set in client side validation users don't need submit form validation results.

this msdn article has illustration of using both server side , client side validation: customvalidator example

i used markup , code-behind , seems work.

markup:

<%@ page language="c#" autoeventwireup="true" codebehind="val.aspx.cs" inherits="customvalidatorexample.val" %> <!doctype html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:label id="lblppsn" runat="server" text="pps number: "></asp:label> <asp:textbox id="tbxppsn" runat="server"></asp:textbox> <asp:customvalidator id="customval_ppsn" runat="server" errormessage="please come in valid ppsn" controltovalidate="tbxppsn" onservervalidate="customval_ppsn_servervalidate" forecolor="red"></asp:customvalidator> <asp:button runat="server" id="button1" text="validate" onclick="button1_click" /> </div> </form> </body> </html>

code-behind:

using system; using system.collections.generic; using system.linq; using system.web; using system.web.ui; using system.web.ui.webcontrols; namespace customvalidatorexample { public partial class val : system.web.ui.page { protected void customval_ppsn_servervalidate(object source, servervalidateeventargs args) { string checkchar = "", formatchar = "", input = tbxppsn.text; bool newformat = false; if (input.length > 9) { args.isvalid = false; return; } if (input.length == 9) { newformat = true; } char[] ppsarray = input.tochararray(); array.reverse(ppsarray); checkchar = ppsarray[0].tostring(); if (newformat) { checkchar = ppsarray[1].tostring(); formatchar = ppsarray[0].tostring(); } checkchar = checkchar.tolower(); formatchar = formatchar.tolower(); int seed = 2, ppsparsed = 0, ppssum = 0, ppsmod = 0; foreach (char ppschar in ppsarray) { if (int.tryparse(ppschar.tostring(), out ppsparsed)) { ppssum += ppsparsed * seed; seed++; } } if (newformat) { ppssum += convert.toint32(formatchar.tochararray()[0] - 96) * 9; } ppsmod = ppssum % 23; if (ppsmod == 0) ppsmod = 23; if (convert.tostring((char)(96 + ppsmod)).tolower() == checkchar) { args.isvalid = true; return; } else { args.isvalid = false; } } protected void button1_click(object sender, eventargs e) { if (page.isvalid) { response.write("valid"); } } } }

c# asp.net customvalidator

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

java - Parsing XML, skip certain tags -

c# - ASP.NET MVC Sequence contains no matching element -