vt673
4
Q:

html form validation

<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

<input type="text" pattern="[a-zA-Z'-'\s]*">
2
<!DOCTYPE html>
<html>
<head>
<script>
function validateForm() {
  var x = document.forms["myForm"]["fname"].value;
  if (x == "") {
    alert("Name must be filled out");
    return false;
  }
}
</script>
</head>
<body>

<form name="myForm" action="/action_page.php" onsubmit="return validateForm()" method="post">
  Name: <input type="text" name="fname">
  <input type="submit" value="Submit">
</form>

</body>
</html>
7
<form action="">
    <label for="name">Username</label>
    <input id="name" type="text" required />
    <label for="Email">Email</label>
    <!-- The required attribute will prompt the user to make sure  -->
    <!-- the fields are not empty, and that the email is formatted correctly -->
    <!-- if you want to use custom validation you should use regex in JavaScript -->
    <input id="Email" type="text" required />
</form>
3

New to Communities?

Join the community