envoke a javascript function if regularexpressionvalidator is true or false

Rate It (1)

Last post 05-25-2007 6:34 AM by Rahabm. 9 replies.

Sort Posts:

  • envoke a javascript function if regularexpressionvalidator is true or false

    05-23-2007, 9:31 PM
    • Loading...
    • Rahabm
    • Joined on 05-22-2006, 8:22 PM
    • Posts 111

    Hello every body

    I am using a regularexpressionvalidator to check and see if an email is correct or not. Now if the email address is correct I want to show a logo (green tick sign) in front of that text box (where the email is typed) otherwise show a red cross sign instead. I also want to show the default error message that regularexpressionvalidator control let me set.

    in front of that email textbox i droped a label, so I need to change the background picture of that label according to regularexpressionvalidator being true/false.

    There is a function (IsValid) but I didnt know how to use it to achieve my goal.

    I appreciate your help and thank you very much.


     

     

    Atlas + ASP .net 2.0 = The best !
  • Re: envoke a javascript function if regularexpressionvalidator is true or false

    05-24-2007, 7:47 AM
    • Loading...
    • Rahabm
    • Joined on 05-22-2006, 8:22 PM
    • Posts 111

    is there any body who know the solution ?

    I appreciate your help. 

    Atlas + ASP .net 2.0 = The best !
  • Re: envoke a javascript function if regularexpressionvalidator is true or false

    05-24-2007, 9:17 AM
    • Loading...
    • alaasw
    • Joined on 01-23-2007, 9:47 PM
    • Calgary, Canada
    • Posts 21
  • Re: envoke a javascript function if regularexpressionvalidator is true or false

    05-24-2007, 9:43 AM
    • Loading...
    • Rahabm
    • Joined on 05-22-2006, 8:22 PM
    • Posts 111

    Thanks for your answer, but this is not what I Am looking for. I only need client side feedback. Right now the regularexpression validator tell me if the email is correct or not, but I want to add an extra feedback to user by showing them a small logo that is activated in client side (javascript). 

    Atlas + ASP .net 2.0 = The best !
  • Re: envoke a javascript function if regularexpressionvalidator is true or false

    05-24-2007, 2:48 PM
    • Loading...
    • isheahan
    • Joined on 05-11-2005, 4:58 AM
    • Posts 125

    Not sure if this is what you are looking for, but perhaps something of this effect might be helpful(?):

    I think, though, that if all you need is client-side feedback, it's not the asp:RegularExpressionValidator control that is going to help you - you may need to create a custom client-side regex snippet for the email validation because the fact that the one you're using is a .net control, it is a server-side object.

    During the page_load event or other event associated with the validation:

     

    1    protected void Page_Load(object sender, EventArgs e)
    2 {
    3 if (Page.IsValid)
    4 Response.Write( /* write out your javascript function here like back in the *olden* days of classic asp*/ );
    5 }
     
     
  • Re: envoke a javascript function if regularexpressionvalidator is true or false

    05-24-2007, 3:50 PM
    • Loading...
    • DeanWagner
    • Joined on 03-27-2006, 6:43 PM
    • Posts 91

    The only way I know of to do this would be to use a CustomValidator.  You would create a javascript function that checks args.Value against the email regex, and set args.IsValid = true or false, and show the graphic as appropriate.

  • Re: envoke a javascript function if regularexpressionvalidator is true or false

    05-25-2007, 1:01 AM
    Answer

    Hi Rahabm,

    Please try this solution. My idea is when the data in TextBox is valid; its visibility property is hidden. Hence, I use the validateCheck function to check the property every time onpropertychange event fires.

    In this demo I use the Label control for your reference. You can replace it with the Image control.

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <script type="text/javascript">
        function validateCheck()
        {
            var rev = document.getElementById('RegularExpressionValidator1');
            if (rev.style.visibility != 'hidden')
            {
                var label = document.getElementById('Label1');
                label.style.backgroundColor='red';
                label.innerText = 'incorrect';
            }
            else
            {
                var label = document.getElementById('Label1');
                label.style.backgroundColor='blue';
                label.innerText = 'correct';
            }  
        }
    </script>

    </head>
    <body>
        <form id="Form1" runat="server">
            <asp:Label ID="Label1" runat="server" Style="position: static"></asp:Label>
            <asp:TextBox ID="TextBox1" runat="server">
            </asp:TextBox>
            <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="TextBox1"
                Display="Static" Font-Names="verdana" Font-Size="10pt" onpropertychange=" validateCheck()"
                ValidationExpression="\d{3}" ForeColor="White">
            </asp:RegularExpressionValidator>
        </form>
    </body>
    </html>

     

    Sincerely,
    Benson Yu
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help. This can be beneficial to other community members reading the thread.
  • Re: envoke a javascript function if regularexpressionvalidator is true or false

    05-25-2007, 5:59 AM
    • Loading...
    • Rahabm
    • Joined on 05-22-2006, 8:22 PM
    • Posts 111

     Thank you Benson, but onpropertychange is not a valid attribute for Regularexpressionvalidator ?

    When I type it a red line show up under it.
     

    Atlas + ASP .net 2.0 = The best !
  • Re: envoke a javascript function if regularexpressionvalidator is true or false

    05-25-2007, 6:12 AM

    Hi Rahabm,

    Don’t worry. The Regularexpressionvalidator render to the client is a <span>. The onpropertychange is for <span>. This code works on my side. So please try. If anything unclear, please let me know.


     

    Sincerely,
    Benson Yu
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help. This can be beneficial to other community members reading the thread.
  • Re: envoke a javascript function if regularexpressionvalidator is true or false

    05-25-2007, 6:34 AM
    • Loading...
    • Rahabm
    • Joined on 05-22-2006, 8:22 PM
    • Posts 111

     Thank you dear Benson.

    I tried it but it seems like the onpropertychange doesnt envoke my javascript function, this is the code:

                <asp:RegularExpressionValidator ID="RegularExpressionValidator1"
                                                runat="server"
                                                ControlToValidate="EmailRegTxtB"
                                                ErrorMessage="Your email is not a valid email!"
                                                Font-Bold="True"
                                                Font-Size="XX-Small"
                                                ForeColor="IndianRed"
                                                onpropertychange="validateCheckemail()"
                                                Style="z-index: 160; left: 166px; position: absolute; top: 170px"
                                                ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">
                </asp:RegularExpressionValidator>

    and this is my javascript code:

        function validateCheckemail()
        {
            var regularev = document.getElementById(prefix + 'RegularExpressionValidator1');
           
            if (regularev.style.visibility == 'hidden')
            {
                  document.getElementById(prefix + 'emaillbl').style.visibility = "visible";
                 document.getElementById(prefix + 'emaillbl').style.backgroundImage = "url('images/icons/correct.png')";
                 document.getElementById(prefix + 'TitleDropD').disabled = false;
                 document.getElementById(prefix + 'TitleDropD').focus();
            }
            else
            {
                document.getElementById(prefix + 'TitleDropD').disabled = true;
                document.getElementById(prefix + 'emaillbl').style.visibility = "visible";
                document.getElementById(prefix + 'emaillbl').style.backgroundImage = "url('images/icons/wrong.png')";
                //document.getElementById(prefix + 'lblMsg5').innerHTML =  'Your email is not a valid email!';
            } 
        } 

     

    the funny thing is that I used normal javascript regular expression validation using test() and everything was working fine apart from internet explorer. it was showing me stupid javascript error, something like object is not referenced or something. but with regular expression validator it doesnt show that error and that is why I want to use it.

    Thanks 

    Atlas + ASP .net 2.0 = The best !
Page 1 of 1 (10 items)