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>

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

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

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.

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.