Question

see the following code :

<html>
    <head>
        <script type="text/javascript">
            function validate()
            {
                fname = f.fn.value;
                if(fname!= "abc")
                    alert("Incorrect name!")

                lname = f.ln.value;
                if(lname != "xyz")
                    alert("Incorrect Name!")

                paswd = f.pswd.value;
                if(paswd<8)
                    alert("Too short password!")

                for(var i=0; i<f.d.length; i++)
                {
                    if(f.d[i].value.checked)
                    {
                        document.write(f.d[i].value);
                    }   
                }

                for(var i=0; i<f.c.length; i++)
                {
                    if(f.c[i].value.checked)
                    {
                        alert(f.c[i].value);
                    }
                }
            }
        </script>
    </head>

    <body>
        <form name="f" onsubmit="validate()">
            First Name: <input type = "text" name = "fn"> <br>
            Last Name: <input type = "text" name = "ln"> <br>
            Password: <input type = "password" name = "pswd"> <br>
            E-mail: <input type = "email" name = "mail"> <br>
            Degree : <input type = "radio" name = "d" value = 's'> SE 
            <input type = "radio" name = "d" value = 'c'>CS 
            <input type = "radio" name = "d" value = 'E'>IT <br>
            University
            <select name = "uni">
                <option value = 'p'>PU</option>
                <option value = 'f'>FAST</option> 
            </select> <br>
            CGPA : <input type = "radio" name = "c" value = '3'> >=3.0
            <input type = "radio" name = "c" value = '2'> >=2.5 <br>
            Browse <input type = "file" name = "uf">  <br>
            <input type = "Submit" value = "Submit">
        </form>
    </body>
</html>

When i press the submit button,I should get a message of

Incorrect name

or

too short password

if the conditions are true but nothing happens, why? Why the

validate()

function not running?

Was it helpful?

Solution

The Problem

int i

makes no sense. This would be proper Java syntax, but not in Javascript. I think you mean var i

What else?

You have two form tags.

PS

If you're too lazy to open your web browser's console (or if it doesn't have one), just use the try and catch expressions.

I'm too lazy to fix all these issues. Just give me the fixed code

DEMO

OTHER TIPS

First of all:

Just use form once:

<form> // <--- remove this line
<form name="f" onsubmit="validate()">

Second, you're using a mixture of what seems like JAVA and JavaScript, so instead of for(int i, declare your variable with var. Like so:

for (var i = 0; i < f.d.length; i++) { <--- var instead of int
    if (f.d[i].value.checked) {
        alert(f.d[i].value);
    }
}

That should remove all the errors, you could have also seen these errors yourself when using the correct debugging tools. Here is a list of them:

Couple of error in your code

 <html>
  <head>
   <script type="text/javascript">
function validate()
{
  fname = document.f.fn.value;
  if(fname!= "abc")
  alert("Incorrect name!");

  lname = f.ln.value;
  if(lname != "xyz");
  alert("Incorrect Name!");
  paswd = f.pswd.value;
  if(paswd<8)
  alert("Too short password!")
   for(i=0; i<f.d.length; i++)
    {
    if(f.d[i].value.checked)
    {
        document.write(f.d[i].value);
    }   
     }
    for(i=0; i<f.c.length; i++)
     {
    if(f.c[i].value.checked)
    {
        alert(f.c[i].value);
    }
      }
   // return false;  // use this you don't want to submit form
   // return true;  //for advancing the form
   }
 </script>
</head>

<body>

<form action="" name="f" onsubmit=" return validate()">
First Name: <input type = "text" name = "fn"> <br>
Last Name: <input type = "text" name = "ln"> <br>
Password: <input type = "password" name = "pswd"> <br>
E-mail: <input type = "email" name = "mail"> <br>
Degree : <input type = "radio" name = "d" value = 's'> SE 
<input type = "radio" name = "d" value = 'c'>CS 
<input type = "radio" name = "d" value = 'E'>IT <br>
University <select name = "uni">
<option value = 'p'>PU</option>
<option value = 'f'>FAST</option> 
</select> <br>
CGPA : <input type = "radio" name = "c" value = '3'> >=3.0
<input type = "radio" name = "c" value = '2'> >=2.5 <br>
Browse <input type = "file" name = "uf">  <br>
<input type = "submit" value = "Submit">
</form>
</body>
</html>

above code is working

I consider that you are performing validation on this form hence you need to the call the function

return validate();

Now if function return false, the form is not submitted

if function return true, the form is submitted

Do ask for further help , Don't waste my effort's

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top