1

I am writing code for validating the money value as

       function monyValid()
       {

       var valw=document.getElementById("<%=txtID4.ClientID%>").value;
       var regex  = /(?:^\d{1,3}(?:\.?\d{2})*(?:,\d{2})?$)|(?:^\d{1,3}(?:,?\d{3})*(?:\.\d{2})?$)/            
       if (!isNaN(valw) && isFinite(valw)) 
       {
         document.getElementById("<%=txtID4.ClientID%>").value=parseFloat(valw).toFixed(2);
       }           
         if (regex.test(valw))
         {          
           alert("valid");
         }
         else
         {
           alert("Number is invalid");
         }
       }

now I want to apply the same validation to multiple textboxes. How can I use the same function for different textboxes. I want something like

      function monyValid(txtVal)
       {           
       var valw=document.getElementById(txtVal).value;
       }

How can I implement this function.

1 Answer 1

1

Pass in this and use it as a parameter in the function instead of calling document.getElementById.

JavaScript

function monyValid(item) {
    var valw = item.value;
    var regex = /(?:^\d{1,3}(?:\.?\d{2})*(?:,\d{2})?$)|(?:^\d{1,3}(?:,?\d{3})*(?:\.\d{2})?$)/
    if (!isNaN(valw) && isFinite(valw)) {
        item.value = parseFloat(valw).toFixed(2);
    }
    if (regex.test(valw)) {
        alert("valid");
    } else {
        alert("Number is invalid");
    }
}

HTML usage

<asp:TextBox ID="txtID4" runat="server" onFocus="onEnter(this)" onblur="onLeave(this);monyValid(this);" TabIndex="4"></asp:TextBox>
Sign up to request clarification or add additional context in comments.

2 Comments

I called the function as <asp:TextBox ID="txtID4" runat="server" onFocus="onEnter(this)" onblur="onLeave(this);monyValid("<%=txtID4.ClientID%>");" TabIndex="4"></asp:TextBox> giving error as Error 12 Server tags cannot contain <% ... %> constructs.
Ah you're using it like that. I changed the answer, think that should work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.