Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Not Checking Validation

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Not Checking Validation

Postby Sunny » Wed Oct 30, 2013 3:00 am

Hi

Form is not checking for Valid Values in Name & Email . What i feel is it is not going in Javascript Code.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Registration Form</title>
<link href="style/style.css" rel="stylesheet" type="text/css" media="all" />
<script type="text/javascript" src="js/script.js" > </script>
</head>

<body onload="document.registration.fname.focus();">
<div id="wrap">
<div id="wrap2">
<h2 class="free_account">Registeration Form</h2>
<form action="process.php" name="registration" onsubmit='return formValidation()' method="post" id="register_form">
<p class="validate_msg">Please fix the errors below!</p>
<p> <label for="name">First Name</label> <input name="fname" type="text" /> <span class="val_fname"></span> </p>
<p> <label for="email">Your Email</label> <input name="email" type="text" /> <span class="val_email"></span> </p>

<input type="submit" name="submit" value="Register">
</form>
</div>
</div>
</body>
</html>

BELOW IS JAVASCRIPT COED
function formValidation()
{
var uname = document.registration.fname;
var uemail = document.registration.email;

if(allLetter(uname))
{
if(ValidateEmail(uemail))
}
}
return false;

}
function allLetter(uname)
{
alert "hello";
var letters = /^[A-Za-z]+$/;
if(uname.value.match(letters))
{
return true;
}
else
{
alert('Username must have alphabet characters only');
uname.focus();
return false;
}
}
function ValidateEmail(uemail)
{
var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
if(uemail.value.match(mailformat))
{
return true;
}
else
{
alert("You have entered an invalid email address!");
uemail.focus();
return false;
}
}
else
{
alert('Form Succesfully Submitted');
window.location.reload()
return true;
}
}



Thanks
Sunny
New php-forum User
New php-forum User
 
Posts: 23
Joined: Wed Nov 30, 2011 6:51 am

Re: Not Checking Validation

Postby Sunny » Wed Oct 30, 2013 9:27 pm

Hi

In the below code it is not performing validation .

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Registration Form</title>
<link href="style/style.css" rel="stylesheet" type="text/css" media="all" />

<SCRIPT type="text/javascript">
alert("hello");
function validate(form) {
OK=false;

var letters = /^[A-Za-z]+$/;
if (document.forms["registration"]["fname"].value.match(letters))
{
OK = true;
}
else
{
OK = false;
}
if(OK) {
form.submit();
}
else {
alert("You did it wrong!");
}
</SCRIPT>
</head>

<div id="wrap">
<div id="wrap2">
<form action="process.php" name="registration" method="post" id="register_form">
<p class="validate_msg">Please fix the errors below!</p>
<p> <label for="name">First Name</label> <input name="fname" type="text" /> <span class="val_fname"></span> </p>
<input type="submit" name="submit" onclick="validate(this.form)" value="Register">
</form>
</div>
</div>
</body>
</html>
Sunny
New php-forum User
New php-forum User
 
Posts: 23
Joined: Wed Nov 30, 2011 6:51 am

Re: Not Checking Validation

Postby nanirao89 » Thu Nov 21, 2013 6:30 am

Hi Sunny,
In the above two codes i have found some errors and common mistakes. Please go through below points.

Code 1:
1) Total "javascript" coding was wrong. No proper close & end. Please read the "javascript" first and go
for writing logics.
2) I dont understand why you have written form action & window.location.reload at same time if form is
submitted correctly.

Code 2:
1) The same mistake in this also you have missed "}" in javascript logic.
2) Your are not handling anything if the form is wrong. So,use logic to stop submitting of form or use
return while calling validate function with using false or true as above in code 1.

Have a nice day 8)
nanirao89
New php-forum User
New php-forum User
 
Posts: 4
Joined: Mon Nov 18, 2013 10:41 pm


Return to PHP coding => General

Who is online

Users browsing this forum: No registered users and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.