Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I am having 2 Textboxes inside gridview named OdometerBegin and OdometerEnd . I have to validate the OdometerEnd Textbox . It should accept numeric values that should be Greater than OdometerStart Textbox. Plz Help Me

Thanks in Advance
shuresh
Posted
Comments
Dasaradhi_r 8-Aug-12 6:09am    
Shuresh,
Do you need to validate values on a user action like clicking a button in a row?
Or you need to extend the behavior of text box control?
shuresh 8-Aug-12 6:19am    
I need to extend it.

XML
<asp:CompareValidator ID="OdometerCompare" runat="server" ForeColor="Red"
     ControlToValidate="txtOdometerEndAdd" ControlToCompare="txtOdometerStartAdd"
     Type="Integer" Operator="GreaterThan" ErrorMessage="Values Should be Greater than OdometerEnd">
    </asp:CompareValidator>
 
Share this answer
 
Comments
Santhosh Kumar Jayaraman 8-Aug-12 7:26am    
is this wat u want?
shuresh 8-Aug-12 7:28am    
yes.. In your Point of view What u think?
Hi..
This is a better solution to extend the behaviour of your text boxes, because
1) You are not allowing the user to enter non numeric values
2) You have better control even if user user makes changes both text boxes regularly.

Say you have columns like the below:
XML
<Columns>
  <asp:TemplateField HeaderText="Odometer Begin">
   <ItemTemplate>
    <asp:TextBox runat="server" ID="OdometerBegin" onkeydown="return jsDecimals(event);" onblur="return validateBegin();" ></asp:TextBox>
    </ItemTemplate>
   </asp:TemplateField>
  <asp:TemplateField HeaderText="Odometer End">
   <ItemTemplate>
    <asp:TextBox runat="server" ID="OdometerEnd" onkeydown="return jsDecimals(event);" onblur="return validateEnd();" ></asp:TextBox>
    </ItemTemplate>
     </asp:TemplateField>
 </Columns>


And use the below Java script functions:

C#
function validateEnd() {
 var beginTxtBox = event.srcElement.id.replace('OdometerEnd', 'OdometerBegin');
 var beginNum = document.getElementById(beginTxtBox).value;
 var endNum = document.getElementById(event.srcElement.id).value;
 if (endNum != '') {
  if (parseInt(endNum) < parseInt(beginNum)){
   alert('Please enter End Number greater than Begin Number');
     return false;
  }
 }
 else {
  alert('Please Enter a valid numeric value');
 }
}
function validateBegin() {
 var endTxtBox = event.srcElement.id.replace('OdometerBegin', 'OdometerEnd');
 var beginNum = document.getElementById(event.srcElement.id).value;
 var endNum = document.getElementById(endTxtBox).value;
 if (beginNum != '') {
   if (parseInt(endNum) < parseInt(beginNum)) {
     alert('Please enter End Number greater than Begin Number');
        return false;
     }
   }
 else {
  alert('Please Enter a valid numeric value');
  }
 }
function jsDecimals(e) {
 var evt = (e) ? e : window.event;
 var key = (evt.keyCode) ? evt.keyCode : evt.which;
 if (key != null) {
   key = parseInt(key, 10);
   if ((key < 48 || key > 57) && (key < 96 || key > 105)) {
      if (!jsIsUserFriendlyChar(key, "Decimals")) {
         return false;
      }
   }
   else {
    if (evt.shiftKey) {
      return false;
    }
   }
 }
  return true;
}
function jsIsUserFriendlyChar(val, step) {
 if (val == 8 || val == 9 || val == 13 || val == 45 || val == 46) {
  return true;
 }
 if ((val > 16 && val < 21) || (val > 34 && val < 41)) {
   return true;
 }
 if (step == "Decimals") {
  if (val == 190 || val == 110) {  
   return true;
  }
 }
 return false;
}
 
Share this answer
 
Those column in the gridview you want to validate convert that columns in to template field (EditItem template) drodown required field validator.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900