Explain Data Validation in DHTML ?
https://www.computersprofessor.com/2016/06/explain-data-validation-in-dhtml.html?m=0
DHTML : Dynamic
HTML
Validation is
simply the process
of ensuring that
some data might be
correct data for a
particular application. Data validation
is the process of
ensuring that users submit only
the set of
characters which you
require.
A common technique
for restricting access
to a site
is to check user
ID’s against a file of
valid users.
Generally the
RegExp are used to
validate the data.
The
Regular expression which checks
that any name entered
by users only
has allowable characters
in it.
What
you cannot do is
check if they have entered
a real name.
Program:
< script
language = “javascript” >
function validate ( )
{
var res = “false”;
var name= document. forms [0].elements [0].value;
var
age =document. forms [0].elements [1]. value;
alert
(name);
alert
(age);
var name_reg=new RegExp("^[A-Z][a-z]+$");
var age_reg=new RegExp("^[\\d]+$");
if (name.match (name_reg))
{
If (age.match (age_reg))
{
alert(“data valid”);
res=”true”;
}
else
alert
(“Name is correct but
age is wrong “+age_reg);
}
}
else
{
alert (“Name does not
match” +name_reg);
}
return res ;
}
</script >
< body >
< form method= “post” action= “c:\" onsubmit = “return validate( )" >
<table cellpadding = “2”>
<tr >
<td> enter name </td>
<td> <input type= “text” > </td>
</tr >
<tr >
<td> enter age </td >
<td> <input type= “password” ></td>
</tr>
</table>
< input type= “submit” value= “ submit” onclick = “return validate ( )” / >
</form >
</body >
Explanation:
Var name = document. Form [0]. Elements [0].value;
The value
from the name field
is copied in to
a local variable.
name_reg=new RegExp(“^[A-Z]+$”, “g”);
The
regular expression checks for
a capital letter
at the start of the line followed by one or more
Characters
until the end of
the line is reached.
age_reg= new RegExp (“^[\\d]+$”, “g”);
The regular expression only accepts
digits 0 to 9 between the start and end of the
string.