Click to See Complete Forum and Search --> : Javascript problem: form validation


fibonacci
05-05-2006, 06:18 AM
heyya..

i got a problem with my javascript form validation..
i have no idea what is the problem as i think i have written the right javascript code..

<?php

require 'f:/www/digitallibrary/conf/config.inc.php';
require 'f:/www/digitallibrary/conf/smarty.inc.php';


?>

<? $html->display('header.html') ?>

<script language="javascript">

function validate_form(form){

if(form.name.value==" " && form.password.value==" "){
alert("Fill out ALL fields.");
return false;
}
return true;
}

</script>


<form name="login" method="post" action="checklogin.php" onsubmit="return validate_form(this)";>


<?
if (isset($_GET['e']) )
{
echo "<font color='red'><b>Wrong Username and Password</b></font>";
}
?>

<table>
<tr>
<td colspan="2"> &nbsp; </td>
</tr>
<tr>
<td colspan="2"> &nbsp; </td>
</tr>
<tr>
<td colspan="2"><b> Login</b></td>
</tr>
<tr>

<td>Username : </td>
<td><input type="text" name="username" id="username" size="15" /></td>
</tr>
<tr>
<td>Password : </td>
<td><input type="password" name="password" id="password" size="15" /></td>
</tr>
<tr>
<td colspan="2"> &nbsp; </td>
</tr>
<tr>
<td colspan="2" align="right"><input type="submit" value="Login" /></td>
</tr>
<tr>
<td colspan="2"> &nbsp; </td>
</tr>
<tr>
<td colspan="2"><h5><a href=#> Forget Your Username and Password? </a></h5></td>
</tr>
</table>

</form>


<? $html->display('footer.html') ?>

is there anything wrong with my javascript code?
anyone can give me a hand?
thanks in advance...

esukf
05-05-2006, 06:35 AM
<script language="javascript">

function validate_form(form){

if(form.username.value=="" || form.password.value==""){
alert("Fill out ALL fields.");
return false;
}
return true;
}

</script>

fibonacci
05-05-2006, 06:55 AM
hi..
thanks esukf it works..!!

anyway, if u don't mind
can u explain what is the different between || and &&

thanks

Houdini
05-05-2006, 07:17 AM
if(form.name.value==" " && form.password.value==" "){
Should be
if(form.username.value=="" && form.password.value==""){
|| is the OR and of course && is AND. Had you used || If either the password OR the name been blank it would have given the Alert. Using AND both have to be blank to get the Alert so your logic actually need to be to work correctly.
if(form.username.value=="" || form.password.value==""){To test and see what I mean enter anything even a space into the username and the script will not show the Alert and carry on to the PHP script.

esukf
05-05-2006, 07:27 AM
Deleted