Javascript validation problem

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Javascript validation problem

Post 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
User avatar
harrisonad
Forum Contributor
Posts: 288
Joined: Fri Oct 15, 2004 4:58 am
Location: Philippines
Contact:

Post by harrisonad »

any script error?

or try this.

Code: Select all

var company = document.form.txtCompany.value;
do also to the rests.
Last edited by harrisonad on Thu Jul 14, 2005 9:31 pm, edited 1 time in total.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Problem solved

Post 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
Post Reply