Tuesday, April 29, 2008

Client side validation on button click using Customvalidator

<asp:Panel ID=pn runat=server  DefaultButton=imagebutton1>
               <table width="620" height="60" cellpadding="0" class=tablesearchborder cellspacing="0">
                      <tr>
                        <td align="center" valign="middle"  background="images/searchnew.gif">
                                     <table width="620" height=55px cellpadding="0" cellspacing="0">
         <tr>
                              <td height="15"></td>
                              <td></td>
                              <td></td>
                              <td></td>
                              <td></td>
                              <td></td>
                            </tr>
       
                            <tr>
                              <td width="1"></td>
                              <td width="7" class="head1"> </td>
                              <td width="36" class="head1"> </td>
                              <td width="67" class="head1">Search</td>
                              <td width="382"><span class="searchboxdata">
                              <asp:TextBox runat=server    ID=ksearch  ValidationGroup=ks  CssClass="txt"></asp:TextBox>
                             
                              </span></td>
                              <td width="83" align=left><asp:ImageButton ID="ImageButton1"  ValidationGroup=ks Width=38 Height=24   ImageUrl="images/gobutton.gif"   OnClick="go"   runat=server/></td>
                            </tr>
                            <tr>
                              <td></td>
                              <td class="head1"> </td>
                              <td class="head1"> </td>
                              <td class="head1"> </td>
                              <td class="marquee">
                               <asp:RadioButtonList ID=rb RepeatColumns=2 ValidationGroup=ks RepeatDirection=Horizontal runat=server>
                           <asp:ListItem Value="KH" >Keyword/Bussiness/category</asp:ListItem>
                           <asp:ListItem Value="PH">Phone</asp:ListItem>
                            </asp:RadioButtonList>
                           </td>
                              <td> </td>
                            </tr>
                        </table>
                      
                        </td>
                      </tr>
                    </table>
                </asp:Panel>
             
             <asp:CustomValidator ID="cu" ValidationGroup=ks runat=server ControlToValidate=ksearch ValidateEmptyText=true ClientValidationFunction="check" ></asp:CustomValidator>
           <script language=javascript>

function check(source, arguments)
{
var box1 = document.getElementById('<%= ksearch.ClientID %>').value;
var box2 = document.getElementById('<%= rb.ClientID+"_0"');
// box2=box2+"_0";
// alert(box2.checked);
if(box2.checked==true)
{
if(box1=="")
{
alert("Please Enter a Keyword, Business or Category");
arguments.IsValid =false;
}
else
arguments.IsValid = true;
}
else
{
if(box1.length11 )
{
alert("Please Enter Phone Number(min:6 digit and max:10) ");
arguments.IsValid =false;
}
else
{ if(IsNumeric(box1))
arguments.IsValid = true;

else
{alert("Please Enter Only numeric Number ");
arguments.IsValid=false;
}
}
}
}
function IsNumeric(strString)
// check for valid numeric strings
{
var strValidChars = "0123456789";
var strChar;
var blnResult = true;

if (strString.length == 0)
return false;

// test strString consists of valid characters listed above
for (i = 0; i < strString.length && blnResult == true; i++)
{
strChar = strString.charAt(i);
if (strValidChars.indexOf(strChar) == -1)
{
blnResult = false;
}
}
return blnResult;
}

No comments :

Post a Comment

AJAX, asp, Asp.net, asp.net and sql server security, Asp.net IntemIndex, C#, Css, DataBinder.Eval, DataKeyNames, Datalist, Datapager, DataSet, DataTable, DropDownList, FindControl, gridview, JavaScript, jquery, Listview, Paging, Regex, RegularExpression, Repeater, Server side validation, Sql Server, timer, timercallback, Validation, XML, xmlnode, XPath