Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validation Function w/ parameters

Status
Not open for further replies.

jshurst

Programmer
Joined
Oct 27, 2004
Messages
1,158
Location
US
I have the following function which validates numbers from an asp.net control (basically masking).

Code:
<script language="javascript">
function IsNumber(source,arguments)
{
  var ValidChars = "0123456789";
  var Char;
  var sText=document.getElementById("ctl00_cphMainContent_txtPID").value;
  for (i = 0; i < sText.length; i++) 
  { 
    Char = sText.charAt(i); 
    if (ValidChars.indexOf(Char) == -1) 
    {
       arguments.IsValid = false;
       return;
    }
  }
  return;
}
    </script>

and call the function by simply putting "IsNumeric" without specifically passing the parameters.

I would like to replace "ctl00_cphMainContent_txtPID" with a variable that I can pass into the function. Can someone tell me how to do this? If I try to add another variable then the function stops working.

Thanks,

J
 
what is "source" supposed to do? you could do this:

Code:
function IsNumber(source,arguments)
{
  var ValidChars = "0123456789";
  var Char;
  var sText=source.value;
  for (i = 0; i < sText.length; i++)
  {
    Char = sText.charAt(i);
    if (ValidChars.indexOf(Char) == -1)
    {
       arguments.IsValid = false;
       return;
    }
  }
  return;
}

and then call it like:

Code:
<input type="text" name="t1" onchange="IsNumber(this);" />

how are you calling the function?



*cLFlaVA
----------------------------
[tt]somebody set up us the bomb![bomb][/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
I simply copied this function from a website.

I'm calling the function like this
Code:
<asp:CustomValidator ID="CustomValidator1" runat="server" [b]ClientValidationFunction="IsNumber"[/b] ControlToValidate="txtPID" ErrorMessage="Only numbers are allowed."></asp:CustomValidator>

I would like to use this as a generic function for other controls on the page that require only numbers to be entered. Rather than just writing the same function again, I would like to pass in the name of the control.
 
maybe you should ask this in the ASP forum, then. The javascript side is the client side. if you need help with that, please copy/paste the code that your block above generates in the browser.



*cLFlaVA
----------------------------
[tt]somebody set up us the bomb![bomb][/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
What do "source" and "arguments" mean? Seems like the function is expecting those arguments but I'm not passing them over. So are they automatically sent? If I remove them then the function stops working.

I would think that this would be more appropriate in this forum. Because if I change the function to a simple alertbox and pass a parameter then it works fine. Not much to do with asp.net, but rather a function question.

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top