To validate multiple Blank Textbox in the Form using JavaScript

Neeks
Posted by Neeks under JavaScript category on | Views : 17317
This is the Java Script for the Validation of Your controls
<script type="text/javascript">
/*
This function is used to provide the document elements as you needed for the operation
*/
function getVal()
{
var elements = new Array();
//This loop will be executed for all the elements you have provided for the operation
for (var i = 0; i < arguments.length; i++)
{
var element = arguments[i];
if (typeof element == 'string')
element = document.getElementById(element);
if (arguments.length == 1)
return element;
elements.push(element);
}
return elements;
}

function testVal()
{
var elem = getVal('Txt1', 'Txt2', 'Txt3');
var strVal = "";
var errorVal = "";

for(var i =0; i < elem.length; i++)
{
if(elem[i].value == "")
{
if(errorVal != "")
errorVal += "\n";
errorVal += elem[i].name;
}
strVal += " " + elem[i].value;
}
if(errorVal == "")
alert(strVal);
else
alert("There are following errors: \n" + errorVal);

}
</script>


Calling the Java Script:
You have to call the JavaScript like
<html>
<head>
</head>
<body>
To validate multiple Blank Textbox in the Form using JavaScript.
<hr>
There may be requirement that you have to validate the Textbox for the site.
<br/>This example will show you how you can create multiple textbox validation

</br>
I have create this Script which will be executed after the blur event of the Third text box
You will get Error for the Blank textbox.
</br><input type="text" id="Txt1" name="First Name"/>
<input type="text" id="Txt2" name = "Middle Name"/>
<input type="text" id="Txt3" name = "Last Name" onblur="testVal()" />
</body

Comments or Responses

Login to post response