Page 1 of 1

Javascript validation problem

Posted: Thu Jul 14, 2005 3:43 am
by raghavan20

<script language="javascript">
<!--
function validateForm(form){
var valid = 1;//initializing form to be valid
alert ("I am inside javascript:");
//getting field values from the form
alert (form.txtName.value);
var name = form.txtName.value;
alert (name);
var company = form.txtCompany.value;
var phone = form.txtTelephone.value;
var email = form.txtEmail.value;
var when = form.txtWhen.value;
var msg = "";

alert (name);
alert (company);

if (name == ""){
msg = msg + ">>Please enter a valid name\n";
valid = 0;
}

if (company == ""){
msg = msg + ">>Please enter your company name!!!\n";
valid = 0;
}

if (phone == ""){
msg = msg + ">>Please enter a valid phone number!!!\n";
valid = 0;
}

if (email == ""){
msg = msg + ">>Please enter a valid email address!!!\n";
valid = 0;
}

if (when == ""){
msg = msg + ">>Please enter when do you want us to call!!!\n";
valid = 0;
}

if (msg == 0){
alert (msg);
}

if (valid == 0){
return "false";
}else{
return "true";
}
}
//-->
</script>

<body>
<table width="700" class = 'bordersOnly' cellspacing="0" style="margin-top:10px; background-color:#000099; color:#FFFFFF">
<tr style="background-color:#999999; "><td></td><td class="Lalign"><h4>Omnisoft - Call Me Back</h4></td></tr>
<tr><td><?php echo $msg; ?></td></tr>
<form name="frmCallBack" method="get" action="" onsubmit="javascript:return validateForm(this)">
On submission of the form, I call the function 'validateForm' which is defined above. The problem is, I try to assign the form values to variables but I dont get anything if I try to display the variables.

For example, I have got five alerts at the first few lines of the function.
It displays the first three alerts but it doesnot display the next two alerts which come next to each other.
they are, alert (name);, alert (company);

It doesnt return true, even then the form submits. You can see the form in action at:
http://raghavan.allhyper.com/callMeBack.php

Posted: Thu Jul 14, 2005 9:25 pm
by harrisonad
any script error?

or try this.

Code: Select all

var company = document.form.txtCompany.value;
do also to the rests.

Posted: Thu Jul 14, 2005 9:30 pm
by Burrito
just set msg equal to something like "you're missing the following information".

then use msg += "your name \n"; etc to add the missing fields.

then check the lenth of the message, if it's longer than whatever "you're missing the following information" is, throw the alert and return false, otherwise return true.

Problem solved

Posted: Sun Jul 17, 2005 4:40 pm
by raghavan20
Its because i have assigned valid = 0 when any of the validation rule fails.
But there should be valid = false in order for the form to work