Q:

javascript validation for login form

<script language = "JavaScript">
        function validate() {
            var username = document.getElementById("username").value;
            var password = document.getElementById("password").value;
            if (username == null || username == "") {
                alert("Please enter the username.");
                return false;
            }
            if (password == null || password == "") {
                alert("Please enter the password.");
                return false;
            }
            alert('Login successful');
            
        } 
</script>
1
<html>
<body>
<form id="form1" runat="server">
    <div class="container">
        <div class="main">
            <h2>
                Javascript Login Form Validation</h2>
            <form id="form_id" method="post" name="myform">
            <label>
                User Name :</label>
            <input type="text" name="username" id="username" />
            <br><br>
            <label>
                Password :</label>
            <input type="password" name="password" id="password" />
            <input type="button" value="Login" id="submit" onclick="validate();" />
            </form>
        </div>
    </div>
    </form>
</html>
</body>
1
<script language="javascript">

function checkEmail() {

    var email = document.getElementById('txtEmail');
    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>
0
<script>  
function validateform(){  
var name=document.myform.name.value;  
var password=document.myform.password.value;  
  
if (name==null || name==""){  
  alert("Name can't be blank");  
  return false;  
}else if(password.length<6){  
  alert("Password must be at least 6 characters long.");  
  return false;  
  }  
}  
1
<script> 
    function GEEKFORGEEKS() { 
        var name = document.forms["RegForm"]["Name"]; 
        var email = document.forms["RegForm"]["EMail"]; 
        var phone = document.forms["RegForm"]["Telephone"]; 
        var what = document.forms["RegForm"]["Subject"]; 
        var password = document.forms["RegForm"]["Password"]; 
        var address = document.forms["RegForm"]["Address"]; 
  
        if (name.value == "") { 
            window.alert("Please enter your name."); 
            name.focus(); 
            return false; 
        } 
  
        if (address.value == "") { 
            window.alert("Please enter your address."); 
            address.focus(); 
            return false; 
        } 
  
        if (email.value == "") { 
            window.alert( 
              "Please enter a valid e-mail address."); 
            email.focus(); 
            return false; 
        } 
  
        if (phone.value == "") { 
            window.alert( 
              "Please enter your telephone number."); 
            phone.focus(); 
            return false; 
        } 
  
        if (password.value == "") { 
            window.alert("Please enter your password"); 
            password.focus(); 
            return false; 
        } 
  
        if (what.selectedIndex < 1) { 
            alert("Please enter your course."); 
            what.focus(); 
            return false; 
        } 
  
        return true; 
    } 
</script> 
0

New to Communities?

Join the community