javascript
Brief description  about Online courses   join in Online courses
View Devendra  Yadav 's Profile

validation is not working please help me to undetstand .i really can not understand validation thank you

<html>
<body>
<form name="theForm" onSubmit="validateFormOnSubmit(theForm)">
<input type="text" name="from">
<input type="text" name="username">
<input type="submit" value="submit"></form>
<script>
function validateFormOnSubmit(theForm) {
var reason = "";

reason = validateUsername(theForm.username);
reason = validatePassword(theForm.pwd);
reason = validateEmail(theForm.email);
reason = validatePhone(theForm.phone);
reason = validateEmpty(theForm.from);

if (reason != "") {
alert("Some fields need correction:\n" reason);
return false;
}

return true;
}
function validateEmpty(fld) {
var error = "";

if (fld.value.length == 0) {
fld.style.background = 'Yellow';
error = "The required field has not been filled in.\n"
} else {
fld.style.background = 'White';
}
return error;
}
</script>
</body>
Asked by Devendra Yadav | Feb 16, 2015 |  Reply now
Replies (4)
View devendra yadav 's Profile
thank you so much sir .its really helped me :)
Feb 19, 2015
View teacher siliconindia 's Profile
Hi Devendra,

*********************************
var letters = /^[A-Za-z]+$/;
var phoneno = /^d{10}$/;
*********************************

Above line of code is called pattern. for example if we want to do mobile no validation then we need such kind of pattern which is used for identifying that the no coming from text box is valid or not. if it is matching then good if not then it tells user to enter correct mobile no.
this pattern(/^d{10}$/;) includes checking of digit,alphanumeric and length of no.



similarly for email-id also we have set of pattern which is used for checking proper email id.

Note :- Pattern is not predefined . it is customizable.

Thanks and Best Regards
Amit
Feb 18, 2015
View devendra yadav 's Profile
thank you so much sir for replay.its really help
sir please explain this 2 lines code

var letters = /^[A-Za-z]+$/;
var phoneno = /^d{10}$/;
and what is put email validation ?

Thank you :)
Feb 18, 2015
View teacher siliconindia 's Profile
Hi Devendra,

Please check the below demo code. It is very easy to understand.If not please tell me, I will try to make you understand.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
function validation()
{
var first = document.getElementById("firstname").value;
var mobile = document.getElementById("mobile").value;

if(first=='')
{
alert('Please Enter Name');
first.focus();
return false;
}

var letters = /^[A-Za-z]+$/;
if(!first.match(letters))
{
alert('Firstname must have alphabet characters only');
document.getElementById("firstname").value='';
document.getElementById("firstname").focus();
return false;
}

if(mobile=='')
{
alert('Please Enter Mobile NO');
document.getElementById("mobile").focus();
return false;
}
var phoneno = /^\d{10}$/;
if(!mobile.match(phoneno))
{
alert('Please Enter Valid Mobile NO');
document.getElementById("mobile").focus();
return false;
}
}
</script>
</head>

<body>

FiratName <input type="text" id="firstname" /><br />
Mobile <input type="text" id="mobile" />

<input type="button" value="click" onclick="return validation()" />
</body>
</html>

Please check it.

Thanks
Amit
Feb 17, 2015