JavaScript Form Validation

previous next


JavaScript can be used to validate input data in HTML forms before sending off the content to a server.


JavaScript Form Validation

JavaScript can be used to validate input data in HTML forms before sending off the content to a server.

Form data that typically are checked by a JavaScript could be:


Required Fields

The function below checks if a required field has been left empty. If the required field is blank, an alert box alerts a message and the function returns false. If a value is entered, the function returns true (means that data is OK):

function checkRequired(field,alertMessage)
{
  with (field)
  {
    if (value==null || value=="")
    {
      alert
(alertMessage)
      return
false
    }
    else
    {
      return
true
    }
  }
}

The entire script, with the HTML form could look something like this:

<html>
<head>

<script type="text/javascript">
function checkRequired(field,alertMessage)
{
  with (field)
  {
    if (value==null || value=="")
    {
      alert
(alertMessage)
      return
false
    }
    else
    {
      return
true
    }
  }
}

function checkForm(form)
{
  with (form)
  {
    if (
checkRequired(user,"User Name Must be Filled Out!")==false)
    {
     
user.focus()
      return false
    }

  }
}
</script>

</head>
<body>
<form
 action=
"http://www.myjavaserver.com/~chaib/web/tutorials/html/jsp/html_greeting_page.jsp"
 onsubmit="return checkForm(this)" method="post">
User Name: <input type="text" name="user" size="30">
<input type="submit" value="Submit">
</form>
</body>
</html
>

 


E-mail Validation

The function below checks if the content has the general syntax of an email.

This means that the input data must contain at least an @ sign and a dot (.). Also, the @ must not be the first character of the email address, and the last dot must at least be one character after the @ sign:

function checkEmail(field,alertMessage)
{
  with (field)
  {
    atPosition = value.indexOf("@")
    dotPosition = value.lastIndexOf(".")
    if (atPosition<1 || dotPosition-atPosition<2)
    {
      alert
(alertMessage)
      return
false
    }
    else
    {
      return
true
    }
  }
}

The entire script, with the HTML form could look something like this:

<html>
<head>

<script type="text/javascript">
function checkRequired(field,alertMessage)
{
  with (field)
  {
    if (value==null || value=="")
    {
      alert
(alertMessage)
      return
false
    }
    else
    {
      return
true
    }
  }
}

function
checkEmail(field,alertMessage)
{
  with (field)
  {
    atPosition = value.indexOf("@")
    dotPosition = value.lastIndexOf(".")
    if (atPosition<1 || dotPosition-atPosition<2)
    {
      alert
(alertMessage)
      return
false
    }
    else
    {
      return
true
    }
  }
}

function checkForm(form)
{
  with (form)
  {
    if (
checkRequired(user,"User Name Must be Filled Out!")==false)
    {
     
user.focus()
      return false
    }

    else if (
checkEmail(email,"Not a Valid Email Address!")==false)
    {
     
email.focus()
      return false
    }

  }
}
</script>

</head>
<body>
<form
 action=
"http://www.myjavaserver.com/~chaib/web/tutorials/html/jsp/html_greeting_page.jsp"
 onsubmit="return checkForm(this)" method="post">
User Name: <input type="text" name="user" size="30"><br>
Email: <input type="text" name="email" size="30"><br>
<input type="submit" value="Submit">
</form>
</body>
</html
>

 


Try it

To see how HTML and JavaScript work, you can only push the submit button, or you can make your own HTML and JavaScript code.

           

 


previous next