PDA

View Full Version : form valid



why not
06-02-2008, 06:09 AM
hi ! i have any code form valid form with javascript . but this code only check empty fields . how to edit this code for Email check ( valid ) - User ( +6 ) - password ( +6 ) . only edit this code . thanks alot .
function checkform() {
// First the normal form validation
if(document.form.user.value=='') {
alert ('[Please complete the "required" field]');
document.form.user.focus();
return false;
}
if(document.form.pass.value=='') {
alert('Please complete the "required" field');
document.form.pass.focus();
return false;
}
if(document.form.mail.value=='') {
alert('Please complete the "required" field');
document.form.mail.focus();
return false;
}
if(document.form.passr.value=='') {
alert('Please complete the "required" field');
document.form.passr.focus();
return false;
}
if(document.form.dis.value=='') {
alert('Please enter image');
document.form.dis.value='';
document.form.dis.focus();
return false;
}

codeexploiter
06-02-2008, 09:11 AM
Check the following code



function validateEmail(elementValue){
var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
return emailPattern.test(elementValue);
}

function trim(str){
return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

function checkform(){
// First the normal form validation
if (trim(document.form.user.value) == '') {
alert('[Please complete the "required" field]');
document.form.user.focus();
return false;
}else if (trim(document.form.user.value).length < 7) {
alert('User name length should be greater than 6');
document.form.user.focus();
return false;
}
if (trim(document.form.pass.value) == '') {
alert('Please complete the "required" field');
document.form.pass.focus();
return false;
}else if(trim(document.form.pass.value).length < 7){
alert('Please enter a password with a length not less than 7');
document.form.pass.focus();
return false
}

if (trim(document.form.mail.value) == '') {
alert('Please complete the "required" field');
document.form.mail.focus();
return false;
}else if (!validateEmail(document.form.mail.value)) {
alert('Please enter a valid email ID');
document.form.mail.focus();
return false
}

if (trim(document.form.passr.value) == '') {
alert('Please complete the "required" field');
document.form.passr.focus();
return false;
}

if (trim(document.form.dis.value) == '') {
alert('Please enter image');
document.form.dis.value = '';
document.form.dis.focus();
return false;
}
return true;
}


In the above code I've introduced two JavaScript function one for validating email ID and another one for trimming a string (removing empty space from left and right side of a string). I've also changed your code a bit so that if the user enters space character in the form fields you can avoid it while validating the inputs.

why not
06-02-2008, 08:34 PM
no not work ! with this edit all checkform disable .
NOTE : on submit is : return checkform()

codeexploiter
06-03-2008, 03:29 AM
I've checked my code and found that the changes I've made are working as it should. It is difficult to say what is the problem without seeing how you've made your page. Anyway I've made a demo page in which I've made one more changes in the checkform function (you missed it I think), there is no checking to match re-entered password I've added that too in the following code



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<style type="text/css">
</style>
<script type="text/javascript">
function validateEmail(elementValue){
var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
return emailPattern.test(elementValue);
}

function trim(str){
return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

function checkform(){
// First the normal form validation
if (trim(document.form.user.value) == '') {
alert('[Please complete the "required" field]');
document.form.user.focus();
return false;
}
else
if (trim(document.form.user.value).length < 7) {
alert('User name length should be greater than 6');
document.form.user.focus();
return false;
}
if (trim(document.form.pass.value) == '') {
alert('Please complete the "required" field');
document.form.pass.focus();
return false;
}
else
if (trim(document.form.pass.value).length < 7) {
alert('Please enter a password with a length not less than 7');
document.form.pass.focus();
return false
}
if (trim(document.form.passr.value) == '') {
alert('Please complete the "required" field');
document.form.passr.focus();
return false;
}else if(trim(document.form.passr.value)!= trim(document.form.pass.value)){
alert('Passwords does not match');
document.form.passr.focus();
return false;
}

if (trim(document.form.mail.value) == '') {
alert('Please complete the "required" field');
document.form.mail.focus();
return false;
}
else
if (!validateEmail(document.form.mail.value)) {
alert('Please enter a valid email ID');
document.form.mail.focus();
return false
}
if (trim(document.form.dis.value) == '') {
alert('Please enter image');
document.form.dis.value = '';
document.form.dis.focus();
return false;
}
return true;
}
</script>
</head>
<body>
<form name="form" action="" method="get" onsubmit="return checkform();">
<label>User </label><input type="text" name="user" /><br/>
<label>Passwd </label><input type="password" name="pass" /><br/>
<label>Re-enter passwd </label><input type="password" name="passr" /><br/>
<label>Email </label><input type="text" name="mail" /><br/>
<label>Image Name </label><input type="text" name="dis" /><br/>
<input type="submit" value="save"/>
</form>
</body>
</html>


Note that the name, action and method attributes of form tag can be changed according to your requirement. Save this code in your PC and browse and try to press the 'save' button you can clearly see that the checkform function invoked correctly.

why not
06-03-2008, 08:13 AM
now worked ! thanks man .