How Validation is used in JavaScript

In this article I have described about required field validator used in JavaScript
  • 2145

JavaScript Validation

JavaScript is helpful to validate data/information given by user before give to the server.

Mainly following are the points which typically are checked by a JavaScript

  • Required fields empty or not
  • A valid e-mail address
  • A valid date
  • Value in a numeric field

Required field  Validator

The following function checks weather user has left required field

function notEmpty()
 {
 var myTextField = document.getElementById('myText');
 if(myTextField.value != "")
 alert("Thank you ! You have entered: " + myTextField.value)
 else
 alert("You didn't enter anytext ! please enter some text?")
 }

Lets take an example as

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

    <title>Required field validator MCN Solution</title>

    <script type="text/javascript">

function notEmpty(){

      var myTextField = document.getElementById('myText');

      if(myTextField.value != "")

            alert("Thank you ! You have entered: " + myTextField.value)

      else

            alert("You didn't enter anytext ! please enter some text?")      

}

</script>

<input type='text' id='myText' />

<input type='button' onclick='notEmpty()' value='VALIDATE' />

</head>

<body>

 

Output

When run it and left blank TextBox then


val1.jpg

 

when we enter some text in TextBox


vali22.jpg

Further Readings

You may also want to read these related articles :

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

© 2020 DotNetHeaven. All rights reserved.