Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, so im trying to validate my ASP.net forum using JavaScript however my alert boxes arent working. Can i not use Alert as its using ASP ? Any help in appreciated thanks :)

JavaScript
<pre>function userValid() {
   
    var email = document.getElementById('<%= txt_eadd.ClientID %>')

   if (email.value == "") {
        alert("Please enter a valid e-mail address.");
        email.focus();
        return false;
    }
    if (email.value.indexOf("@", 0) < 0) {
        alert("Please enter a valid e-mail address.");
        email.focus();
        return false;
    }
    if (email.value.indexOf(".", 0) < 0) {
        alert("Please enter a valid e-mail address.");
        email.focus();
        return false;
    }
    return true;
}
Posted
Comments
dan!sh 25-Jan-16 5:46am    
Have tried to debug this function?
F-ES Sitecore 25-Jan-16 6:19am    
Use the built in asp.net validation controls, they support client validation as well as server validation. If you validate on the client you have to validate on the server too.
John C Rayan 25-Jan-16 6:40am    
Follow F-ES Sitecore's advise. Hope you are not using classic ASP.
aarif moh shaikh 25-Jan-16 7:27am    
how you calling your function?
ZurdoDev 25-Jan-16 8:22am    
Debug it and you'll see what is happening.

1 solution

Alert always works; you simply never call it, because your conditions in "if" never true, or, very likely, you simply get an exception on the first condition. If you don't handle exceptions, the code terminates silently.

For example, consider what happens if your element email is not found. In this case, document.getElementById returns null:
Document.getElementById — Web APIs | MDN[^].

An attempt to dereference null object reference by taking value throws an exception. You can find out what exactly happens if you use the JavaScript debugger.

—SA
 
Share this answer
 
v3

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