简体   繁体   中英

To add condition java script in aspx.cs

Anyone can help me to add java script condition in aspx.cs?

When update text in email text box and click button update, the java script will call to check validation.

If Email address valid message box will pop out successful update .

If email address not valid message box will pop out not successful update, please provide a valid email .

<script type="text/javascript">    
    function checkEmail() {    
        var email = document.getElementById('btnMod');
        var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

        if (!filter.test(email.value)) {
            alert('Please provide a valid email address');
            email.focus;
            return false;
          }
        }
</script>

<asp:Button ID="btnMod" runat="server" Text="Update" onClick="btnMod_Click" 
OnClientClick='Javascript:checkEmail();'/>     

 protected void btnMod_Click(object sender, EventArgs e)
    {
       base.scriptAlert("Successfully Update.");
    }

What should i put in my cs code when email address not valid?

You can use below code, but I suggest you to use jquery.validation plugins:

function checkEmail() {

    var email = document.getElementById('btnMod');
    var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

    if (!filter.test(email.value)) {
        alert('Please provide a valid email address');
        email.focus;
        return false;
    }

    return true;
}
</script>

And update this as well:

<asp:Button ID="btnMod" runat="server" Text="Update" onClick="btnMod_Click" OnClientClick='Javascript:return checkEmail();'/>  

And CS:

protected void btnMod_Click(object sender, EventArgs e)
{
   Response.Write("<script>alert('Successfully Update.');</script>");
}   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM