0

Please help me with my follwing code.
this can only check if username textbox is empty.
how do i also make it so that it checks if password textbox is empty and thereby give an error??? please help.

<html>
<script type="text/javascript">
function validateFormOnSubmit(strng){
if(strng.value=="")
{
alert("Empty");
return false;
}
else
{
return true;
}
}
</script>
</head>
<body>
<form onsubmit="return validateFormOnSubmit(textBox1);" method="post">
User Name <input type="text" name="textBox1" id="textBox1" /> <br>
Password <input type="password" name="txtPassword" /> <br>
<input type="submit" value="Ok" />
<input type="button" value="Cancel" />
</form>
</body>
</html>

Edited by rukshilag: n/a

3
Contributors
5
Replies
7
Views
7 Years
Discussion Span
Last Post by Atli
0

Change your line 17 so that it reads like......

form onsubmit="return validateFormOnSubmit(textBox1) && return validateFormOnSubmit(txtPassword);" method="post">

OR....

form onsubmit="return validateFormOnSubmit(textBox1) && validateFormOnSubmit(txtPassword);" method="post">

Something like that ought to work....

hope it helps

Edited by mike_2000_17: Fixed formatting

0

sorry but it still doesnt work :S

Change your line 17 so that it reads like......

form onsubmit="return validateFormOnSubmit(textBox1) && return validateFormOnSubmit(txtPassword);" method="post">

OR....

form onsubmit="return validateFormOnSubmit(textBox1) && validateFormOnSubmit(txtPassword);" method="post">

Something like that ought to work....

hope it helps

Edited by mike_2000_17: Fixed formatting

0

Hey.

I would recommend, rather than pile all the input boxes you want validated into the form's onsubmit event attribute, have the callback function pull them out for you.

Like:

<!DOCTYPE html>
<html>
<head>
    <title>Test</title>
    <script type="text/javascript">
        function validateFormOnSubmit(form){
            var textBox = form.textBox1;
            var passwordBox = form.txtPassword;

            if(textBox.value == '') {
                alert('Name is missing');
                return false;
            }
            else if(passwordBox.value == '') {
                alert('Password is missing');
                return false;
            }
            else{
                return true;
            }
        }
    </script>
</head>
<body>
    <form onsubmit="return validateFormOnSubmit(this);" method="post" action="?">
        User Name <input type="text" name="textBox1" id="textBox1" /> <br>
        Password <input type="password" name="txtPassword" /> <br>
        <input type="submit" value="Ok" />
        <input type="button" value="Cancel" />
    </form>
</body>
</html>

This way you can alter the client-side logic without having to mess with the HTML markup. Separates the JavaScript from the markup.

0

what does this mean
action="?"
why do we put that?

Hey.

I would recommend, rather than pile all the input boxes you want validated into the form's onsubmit event attribute, have the callback function pull them out for you.

Like:

<!DOCTYPE html>
<html>
<head>
    <title>Test</title>
    <script type="text/javascript">
        function validateFormOnSubmit(form){
            var textBox = form.textBox1;
            var passwordBox = form.txtPassword;

            if(textBox.value == '') {
                alert('Name is missing');
                return false;
            }
            else if(passwordBox.value == '') {
                alert('Password is missing');
                return false;
            }
            else{
                return true;
            }
        }
    </script>
</head>
<body>
    <form onsubmit="return validateFormOnSubmit(this);" method="post" action="?">
        User Name <input type="text" name="textBox1" id="textBox1" /> <br>
        Password <input type="password" name="txtPassword" /> <br>
        <input type="submit" value="Ok" />
        <input type="button" value="Cancel" />
    </form>
</body>
</html>

This way you can alter the client-side logic without having to mess with the HTML markup. Separates the JavaScript from the markup.

0

what does this mean
action="?"
why do we put that?

It submits the form to the current URL, plus a ?.
So a form on a page like this: www.example.com/form.php submits to: www.example.com/form.php? .

You could also use $_SERVER['PHP_SELF'] if you would rather not add the "?". Makes no difference in the example I posted.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.