ASP . NET Custom Client-Side Validation

Client-Side Validation

I have a custom validation function in JavaScript in a user control on a .Net 2.0 web site which checks to see that the fee paid is not in excess of the fee amount due.

I've placed the validator code in the ascx file, and I have also tried using Page.ClientScript.RegisterClientScriptBlock() and in both cases the validation fires, but cannot find the JavaScript function.

The output in Firefox's error console is "feeAmountCheck is not defined". Here is the function (this was taken directly from firefox->view source)


<script type="text/javascript">
    function feeAmountCheck(source, arguments)
    {
        var amountDue = document.getElementById('ctl00_footerContentHolder_Fees1_FeeDue');
        var amountPaid = document.getElementById('ctl00_footerContentHolder_Fees1_FeePaid');

        if (amountDue.value > 0 && amountDue >= amountPaid)
        {
            arguments.IsValid = true;
        }
        else
        {
            arguments.IsValid = false;
        }

        return arguments;
    }
</script>

Any ideas as to why the function isn't being found? How can I remedy this without having to add the function to my master page or consuming page?

4 Answers

Try changing the argument names to sender and args. And, after you have it working, switch the call over to ScriptManager.RegisterClientScriptBlock, regardless of AJAX use.

When you're using .Net 2.0 and Ajax - you should use:

ScriptManager.RegisterClientScriptBlock

It will work better in Ajax environments then the old Page.ClientScript version

Also you could use:

var amountDue = document.getElementById('<%=YourControlName.ClientID%>');

That will automatically resolve the client id for the element without you having to figure out that it's called 'ctl00_footerContentHolder_Fees1_FeeDue'.

While I would still like an answer to why my javascript wasn't being recognized, the solution I found in the meantime (and should have done in the first place) is to use an Asp:CompareValidator instead of an Asp:CustomValidator.

ScriptManager.RegisterClientScriptBlock Method

Registers a client script block with the ScriptManager control for use with a control that is inside an UpdatePanel control, and then adds the script block to the page.

Overloads

RegisterClientScriptBlock(Control, Type, String, String, Boolean)

Registers a client script block with the ScriptManager control for use with a control that is inside an UpdatePanel control, and then adds the script block to the page.

RegisterClientScriptBlock(Page, Type, String, String, Boolean)

Registers a client script block with the ScriptManager control for use with a control that is inside an UpdatePanel control, and then adds the script block to the page.

Syntax (RegisterClientScriptBlock(Control, Type, String, String, Boolean))

C#
public static void RegisterClientScriptBlock (System.Web.UI.Control control, Type type, string key, string script, bool addScriptTags);

Parameters

control

Control. The control that is registering the client script block.

type

Type. The type of the client script block. This parameter is usually specified by using the typeof operator (C#) or the GetType operator (Visual Basic) to retrieve the type of the control that is registering the script.

key

String. A unique identifier for the script block.

script

String. The script.

addScriptTags

Boolean. true to enclose the script block in <script> and </script> tags; otherwise, false.

Description

Registers a client script block with the ScriptManager control for use with a control that is inside an UpdatePanel control, and then adds the script block to the page.

You use the RegisterClientScriptBlock method to register a client script block that is compatible with partial-page rendering and that has no Microsoft Ajax Library dependencies. Client script blocks that are registered by using this method are sent to the page only when control represents a control that is inside an UpdatePanel control that is being updated. To register a script block every time that an asynchronous postback occurs, use the RegisterClientScriptBlock(Page, Type, String, String, Boolean) overload of this method.

If you want to register a script block that does not pertain to partial-page updates, and if you want to register the script block only one time during initial page rendering, use the RegisterClientScriptBlock method of the ClientScriptManager class. You can get a reference to the ClientScriptManager object from the ClientScript property of the page.

Syntax (RegisterClientScriptBlock(Page, Type, String, String, Boolean))

C#
public static void RegisterClientScriptBlock (System.Web.UI.Page page, Type type, string key, string script, bool addScriptTags);

Parameters

page

Page. The page object that is registering the client script block.

type

Type. The type of the client script block. This parameter is usually specified by using the typeof operator (C#) or the GetType operator (Visual Basic) to retrieve the type of the control that is registering the script.

key

String. A unique identifier for the script block.

script

String. The script to register.

addScriptTags

Boolean. true to enclose the script block in <script> and </script> tags; otherwise, false.

Description

Registers a client script block with the ScriptManager control for use with a control that is inside an UpdatePanel control, and then adds the script block to the page.

When you register a script block by using this method, the script is rendered every time that an asynchronous postback occurs. To register a script block for a control that is inside an UpdatePanel control so that script is registered only when the UpdatePanel control is updated, use the RegisterClientScriptBlock(Control, Type, String, String, Boolean) overload of this method.

If you want to register a script block that does not pertain to partial-page updates, and if you want to register the script block only one time during initial page rendering, use the RegisterClientScriptBlock method of the ClientScriptManager class. You can get a reference to the ClientScriptManager object from the ClientScript property of the page.

Document.getElementById()

An Element object describing the DOM element object matching the specified ID, or null if no matching element was found in the document.

Syntax

getElementById(id)

Parameters

id

The ID of the element to locate. The ID is case-sensitive string which is unique within the document; only one element may have any given ID.

Description

The Document method getElementById() returns an Element object representing the element whose id property matches the specified string. Since element IDs are required to be unique if specified, they're a useful way to get access to a specific element quickly.

If you need to get access to an element which doesn't have an ID, you can use querySelector() to find the element using any selector.

Multiply | JavaScript (solved) | codewars

Multiply - JavaScript (Solved) - codewars

This code does not execute properly. Try to figure out why. Solution function multiply(a, b) { return a * b; } Multiplication The multiplication of whole numbers may be thought of as repeated addition; that is, the multiplication of two numbers is equivalent to adding as many copies of one of them, the multiplicand, as the quantity of the other one, the multiplier. Both numbers can be referred to as factors. Example: 3 * 4 = 4 + 4 + 4 = 12 return The return statement ends function execution and specifies a value to be returned to the function caller. Syntax return [expression]; Parameters expression The expression whose value is to be returned. If omitted, undefined is returned instead. Description When a return statement is used in a function body, the execution of the function is stopped. If specified, a given value is returned to the function caller. How to detect which one of the defined font was…

Read more…

How to detect which one of the defined font was used in a web page?

How to detect font was used

Suppose I have the following CSS rule in my page: body { font-family: Calibri, Trebuchet MS, Helvetica, sans-serif; } How could I detect which one of the defined fonts were used in the user's browser? For people wondering why I want to do this is because the font I'm detecting contains glyphs that are not available in other fonts. If the user does not have the font, then I want it to display a link asking the user to download that font (so they can use my web application with the correct font). Currently, I am displaying the download font link for all users. I want to only display this for people who do not have the correct font…

Read more…

Even or Odd | JavaScript (solved) | codewars

Even or Odd - JavaScript (Solved) - codewars

Even or Odd in JavaScript (Solved) Create a function that takes an integer as an argument and returns "Even" for even numbers or "Odd" for odd numbers. Solution function even_or_odd(number) { return number % 2 ? "Odd" : "Even"; } Even Numbers Any integer that can be divided exactly by 2 is an even number . The last digit is 0, 2, 4, 6 or 8 Example: −24, 0, 6 and 38 are all even numbers Odd Numbers Any integer that cannot be divided exactly by 2 is an odd number . The last digit…

Read more…