• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1882
  • Last Modified:

Custom Validation not firing javascript in FireFox

I have created with help from this site, an ASP.NET page that validates numerious textarea fileds for numeric input, If they user enters text instead of a number the error message is to appear and the textarea is to turn light red. Everything works like a charm in IE, but the javascript will not fire in Firefox. Note that I have an alert call at the top of the javascript which is not firing wither so the function itself is never being called. All of the key code snippets are below:


---------javascript ---------------
<script language="javascript">
          function ValidateHours(sender, args) {
             
            alert("testing")
             
              var ivalError = 0;
              if (document.getElementById("txtDrvHrsMon").value-Math.floor(document.getElementById("txtDrvHrsMon").value) == 0)
               {
                              document.getElementById("txtDrvHrsMon").style.backgroundColor = "white";
               }  else
               {
                              document.getElementById("txtDrvHrsMon").style.backgroundColor = "pink";
                              ivalError++              
               }
                                   
               if (ivalError > 0) {                              
                 args.IsValid = false;                        
                 return    
               }else{
                         args.IsValid = true;              
               }
          }
</script>


--------- Control on page -----------------
<asp:textbox id="txtDrvHrsMon" runat="server" Columns="5">0</asp:textbox>


---------- Validation control -----------------
<asp:customvalidator id="valAllHrs" runat="server" Display="Static" ErrorMessage="Hours must be in numeric format."
ClientValidationFunction="ValidateHours"></asp:customvalidator>
0
cbittner
Asked:
cbittner
  • 2
  • 2
  • 2
2 Solutions
 
laotzi2000Commented:
That's because the customvalidaiton control missed(maybe intentionally) several import elements when rendering on Firefox browser. I guess this must be Microsoft's strategy to beat other browsers.
Don't think you can have much to do about it.

A simple solution is to copy the code that are rendered in IE but not in Firefox to the
.aspx file to replace the customvalidation control
0
 
laotzi2000Commented:
another solution:
1.get rid of the customvalidation control
2.add the following attribute to form: onsubmit="return ValidateHours();"
3.modify your validation function to as follows:
            <script language="javascript">
          function ValidateHours(sender, args) {
             
           alert("testing")
             
              var ivalError = 0;
              if (document.getElementById("txtDrvHrsMon").value-Math.floor(document.getElementById("txtDrvHrsMon").value) == 0)
               {
                         document.getElementById("txtDrvHrsMon").style.backgroundColor = "white";
               }  else
               {
                         document.getElementById("txtDrvHrsMon").style.backgroundColor = "pink";
                         ivalError++              
               }
                                   
               if (ivalError > 0) {                              
                 return false;                      
             
               }else{
                     return true;
                 
               }
               return false;
          }
            </script>
0
 
b1xml2Commented:
keep the client validation,

add the server validation
<asp:customvalidator id="valAllHrs" runat="server" Display="Static" ErrorMessage="Hours must be in numeric format."
ClientValidationFunction="ValidateHours_ClientValidate" OnServerValidate="ValidateHours_ServerValidate"></asp:customvalidator>

  <script language="javascript">
  function ValidateHours_ClientValidate(sender, args)
  {
      var valid = isValidHours();
      getHoursElement().style.backgroundColor = valid ? "white" : "pink";
      args.IsValid = valid;
}
         

function getHoursElement()
{
      return document.getElementById("txtDrvHrsMon");
}

function isValidHours()
{
      var el = getHoursElement();
      return (parseInt(el.value) - Math.floor(el.value) == 0);
}
</script>


<script language="VB" runat="server">
Sub ValidateHours_ServerValidate(ByVal sender As Object, ByVal args As ServerValidateEventArgs)
Dim value As Boolean = IsValidHours
txtDrvHrsMon.Style("background-color",CStr(IIf(value,"white","pink")))
args.IsValid = value
End Sub

ReadOnly Property IsValidHours() As Boolean
      Get
            Return (CInt(txtDrvHrsMon.Text) - CType(Math.Floor(CDbl(txtDrvHrsMon.Text)), Integer) = 0)
      End Get
End Property
</script>
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
cbittnerAuthor Commented:
I am trying the code b1XML2 added, but I am getting an error because both javascript, and VB are on the same aspx page. Am I missing something obvious? which does seem to be a hobby of mine.
0
 
b1xml2Commented:
show us what u have and by the way, don't feel down.

In fact because you are trying to understand how things work, you will get there. It is far better to face difficulties when trying to understand rather than being given the solution without trying or wanting to understand. So, a big pat on your back for your efforst.
0
 
cbittnerAuthor Commented:
I will respond back to this but I had to move to other screens, so I'll be back to this issue in a few DAZE
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now