0

I am trying to run a javascript code but it is not running. I don't know why? Here is the code sample:

Javascript:
<Script Language="text/Javascript">
function validateForm()
{
var x=document.forms["form1"]["Injection"].value;
var y=document.forms["form1"]["limit"].value;
if (x==null || x=="")
  {
  alert("Injection must be filled out");
  return false;
  }
if (y==null || y=="")
  {
  alert("Limit must be filled out");
  return false;
  }
  return true;
}
</Script>

HTML:

        <form  name="form1" method="post" action="/OT_Stock/addnewinjection.jsp" onsubmit="return validateForm()"> 

        <input type="text" name="Injection" maxlength="100" size="20" value="" style="textfield">
         <input type="text" name="limit" maxlength="100" size="20" value="" style="textfield" >



        <input type="submit"  value="Add Name" class="buttonmain">

Please tell me where I am going wrong?

2 Answers 2

2
  1. Remove the onclick from the button
  2. Add onsubmit="return validateForm()" to the form element.

The crucial bit is the return keyword. Without it the form will always submit. With it, when the validateForm method returns false (invalid form) the form will not submit.


Edit: OP has updated his question with the above suggestions.

Sign up to request clarification or add additional context in comments.

6 Comments

I made all changes still its not working. Why Please tell me??
@Samiksha - works for me: jsfiddle.net/pRVP5 - check other javascript is not erroring on your page. If you want further help define "not working".
There is no other javascript on my page. Not working means even though I click submit with blank fields the page is not returning.
@Samiksha - What do you mean "the page is not returning"? Returning what? If you click submit with blank fields a javascript alert should popup and the page not submit... thats what your code implies you're trying to achieve!
Ya that's right but page is submitting to the page specified even though i am submitting with blank fields. Don't know why?
|
0

forma name is " form1", instead of it should be "form1".
Don't use language in the script tag instead use type='text/javascript'.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.