Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the following function

JavaScript
function validateEmail() {
        var emailID = document.form1.txtUEmail.value;
        atpos = emailID.indexOf("@");
        dotpos = emailID.lastIndexOf(".");
        if (atpos < 1 || (dotpos - atpos < 2)) {
            alert("Please enter correct email ID")
            document.form1.txtUEmail.focus();
            return false;
        }
        return (true);
    }

and the text field is
ASP.NET
<asp:TextBox ID="txtUEmail" runat="server" CssClass="input"></asp:TextBox>


and calling statment is
C#
btnSave.Attributes.Add("onclick", "javascript:validateEmail()");

But i does not working means does not generate the alrt and save the record is a enter it. Help is needed
Posted
Updated 16-Aug-15 21:45pm
v3
Comments
Abhinav S 17-Aug-15 3:41am    
What does not work? Do you get an error?
Altaful Haq 17-Aug-15 3:46am    
does not generate the alrt and save the record is i enter it

1 solution

I have tried adding events using the jQuery Attr function before and it doesn't seem to work.

I think the reason must be that the client has to add the event in a particular way or the fact that it's a function gets lost.

Try:
JavaScript
btnSave.onclick = validateEmail;

or
JavaScript
btnSave.onclick = function(){
 validateEmail();
}



If that doesn't work, let me know and I'll look further ^_^

Andy

UPDATE:

Via Jquery this is much easier:

JavaScript
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.js"></script>
<script type="text/javascript">

JavaScript
;
function validateEmail() {
        var emailTxt = $("input[id='txtUEmail']")
        var emailID = $(emailTxt).val();
        atpos = emailID.indexOf("@");
        dotpos = emailID.lastIndexOf(".");
        if (atpos < 1 || (dotpos - atpos < 2)) {
            alert("Please enter correct email ID")
            $(emailTxt).focus();
            return false;
        }
        return (true); //Not the best way to submit btw, but that's a different issue
    }

$(function(){  //jquery shorthand for document.ready
    var saveButton = $("button[id='btnSave']"); //This is a jquery selector

    $(saveButton).click(function(){
        validateEmail();
    });
});

</script>


Your next step is to debug the javascript. Chrome has a great one build it (just hit F12). IE has one but it's not my fav. Firefox requires the FireBug extension install, but it's pretty good.

Hope that helps ^_^

Andy
 
Share this answer
 
v6
Comments
Altaful Haq 17-Aug-15 4:10am    
it does not working Andy. Can you share any other techniques. like ajax or jquery
Andy Lanng 17-Aug-15 4:13am    
Oh yeah. Thanks for asking - it's the preferred method. I will update my solution
Andy Lanng 17-Aug-15 4:25am    
There we go. Code tags were messing up the text. Should read better now. Check the UPDATE section above ^_^

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900