ID validation using JavaScript
ID validation using JavaScript
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title> Form Validation</title>
<script>
function display_msg(fID,msg,color,eID)
{
var fID = document.getElementById(fID) || document.getElementsByName(fID);
var eID = document.getElementById(eID);
eID.innerHTML = msg;
eID.style.color = color;
eID.style.visibility = 'visible';
}
//ID check
//XX-XXXXX-X something like that
function idValidation() {
var idField = document.getElementById('idField');
if (idField.value == "") {
display_msg("idField", "Required", "red", "msg");
}
else {
// display_msg("idField", "Done", "green", "msg");
var x = idField.value;
var y = x.split("-");
if (y.length == 3) {
if (y[0].length == 2 && y[1].length == 5 && y[2].length == 1) {
var isdigit = y[0] + y[1] + y[2];
var i;
var id;
for(i=0;i<isdigit.length;i++)
{
if (isdigit[i] >= '0' && isdigit[i] <= '9')
{
id = "number";
}
else {
id = "not number";
}
}
if (id == "number")
{
display_msg("idField", "Vaid ID", "green", "msg");
}
else {
display_msg("idField", "Invalid ID", "red", "msg");
}
}
else {
display_msg("idField", "Invalid ID", "red", "msg");
}
}
else {
display_msg("idField", "Invalid ID", "red", "msg");
}
//OR you can use this code
/* var X1 = y[0]; //13
var X2 = y[1]; //24572
var X3 = y[2]; //2
if (isDigit(X1[0]) == true && isDigit(X1[1]) == true && isDigit(X2[0]) == true && isDigit(X2[1]) == true && isDigit(X2[2]) == true
&& isDigit(X2[3]) == true && isDigit(X2[4]) == true && isDigit(X3[0]) == true) {
display_msg("idField", "Valid ID", "green", "msg");
}
else {
display_msg("idField", "Invalid ID", "red", "msg");
}
}
else {
display_msg("idField", "Invalid ID", "red", "msg");
}
}
else {
display_msg("idField", "Invalid ID", "red", "msg");
}*/
}
}
function isDigit(d)
{
if(d>='0' && d<='9')
{
return true;
}
return false;
}
function hide(msg_hide_id)
{
var error_msg = document.getElementById(msg_hide_id).style.visibility = "hidden";
}
function submit_Form()
{
idValidation();
}
</script>
</head>
<body>
<form>
<br />
<br />
<input type="text" id="idField" onclick="hide('msg')"/>
<span id="msg"></span> <br /><br />
<input type="button" value="SUBMIT" onclick="idValidation()"/>
</form>
</body>
</html>
//Try this Code
http://poragfaisal.blogspot.com
স্বপ্নের প্রতিবিম্ব...
মন্তব্যসমূহ
একটি মন্তব্য পোস্ট করুন