Solved

How to call asp.net controls validation in javascript for specific control or group?

Posted on 2008-10-29
4
906 Views
Last Modified: 2012-05-05
Hi Expert!,

I have few asp:textbox with requiredfield validator bind on different validation group and different button into the same page.

What i need is when user click on button1
 1) First validate controls over asp.net validator for the specific group.
 2) Then if no error, call a javascript method (who call webservices) and wait for response
 3) No server event postback should occur cause i'm waiting for webservice response.

All the webservices jobsworks fine. My problem now is I need to add "RequiredFieldValidator" on some  controls and i can't find how to call the validation by javascript like the asp:button does.

Anybody can help?
0
Comment
Question by:jabcoco
  • 2
  • 2
4 Comments
 
LVL 22

Accepted Solution

by:
prairiedog earned 500 total points
ID: 22841043
In your JavaScript, call this function:
ValidatorEnable(val, true);
val: the validator ID.
If you need to trigger several validators, then you have to call the above functions several times: one validator at a time.
0
 
LVL 10

Author Comment

by:jabcoco
ID: 22846568
Perfect!!! I also found that "ValidatorValidate" do the same jobs.

Below is a complete exemple base on Prairiedog solution :)

Thanks Sage Prairiedog!!
<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>Untitled Page</title>
 

    <script language="javascript" type="text/javascript">

        function CustomFormValidation()

        {   

            //Simly use ValidatorValidate or ValidatorEnable

            var aspxCtrl = document.getElementById("<%= reqFdlUser.ClientID %>");

            ValidatorEnable(aspxCtrl);

            if(!aspxCtrl.isvalid)

                return false;

                

            aspxCtrl = document.getElementById("<%= reqFdlPass.ClientID %>");

            ValidatorValidate(aspxCtrl);

            if(!aspxCtrl.isvalid)

                return false;                

                

            return true;

        }

    </script>    

</head>
 

<body>

    <form id="form1" runat="server">

    <div>

            <asp:TextBox ID="txtMUser" runat="server"></asp:TextBox><br />

            <asp:TextBox ID="txtMPass" runat="server"></asp:TextBox><br />

            <asp:RequiredFieldValidator ControlToValidate="txtMUser" ID="reqFdlUser" runat="server" ErrorMessage="RequiredFieldValidator" Display="Dynamic"></asp:RequiredFieldValidator>

            <asp:RequiredFieldValidator ControlToValidate="txtMPass" ID="reqFdlPass" runat="server" ErrorMessage="RequiredFieldValidator" Display="Dynamic"></asp:RequiredFieldValidator>

            <input id="btnTest" onclick="if(CustomFormValidation())alert('Form is valid');" type="button" value="Register"/>

    </div>

    </form>

</body>

</html>

Open in new window

0
 
LVL 10

Author Comment

by:jabcoco
ID: 22846640
Solution for validate a specific group without having to work with the "clientid".

Here is a function i've just create in javascript which you can pass a validationgroupname and it returns true or false depends if the validation is correct!!

ENJOY!!! :)


        function ValidateCtrlByGroup(psValidationGroupName)

        {   

            var bResult = true;

            for(i=0;i < Page_Validators.length; i++){

                ctrlValidator = Page_Validators[i];

                if(psValidationGroupName == ctrlValidator.validationGroup){

                    ValidatorEnable(ctrlValidator);        

                    if(!ctrlValidator.isvalid)

                        bResult = false;

                }

            }

            return bResult;

        }

    

Open in new window

0
 
LVL 22

Expert Comment

by:prairiedog
ID: 22850037
Great job, jabcoco!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now