Check out example codes for "html form validation". It will help you in understanding the concepts better.

Code Example 1

<!DOCTYPE html>
function validateForm() {
  var x = document.forms["myForm"]["fname"].value;
  if (x == "") {
    alert("Name must be filled out");
    return false;

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


Code Example 2

<input type="text" pattern="[a-zA-Z'-'\s]*">

Code Example 3

<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');

Code Example 4

<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 />

Learn ReactJs, React Native from