Java script for input validation redirects to the next page

Hello experts,

I've got a problem with my java script, which is used for text inout validation.  It looks as if there was some conflict with submit button.

Will anybody be able to tell me what is wrong?


<tr>
<td align="left"><b>Company phone number</b></td>
<td align=top><input type="text" name="companyPhoneNo" size="20"
      value="<jsp:getProperty name ="customer" property="companyPhoneNo"/>"
      onKeypress="if (event.keyCode < 45 || event.keyCode > 57) event.returnValue = false;">
</td>
</tr>

...
<tr><td align="left"><b>Expiation date</b></td>
<td><input type="text" name="creditCardExpirationDate"
        value="<jsp:getProperty name ="customer" property="creditCardExpirationDate"/>"
        onKeypress="if (event.keyCode < 45 || event.keyCode > 57) event.returnValue = false;">
</td>
</tr>
...
<input type="submit" name="submitForm" value="Save Details" onClick="validate(this.form)">
<input type="reset" name="reset" value="Reset">


I am also using java script in the head of the jsp page, which is checking if the required input boxes are not empty

I do not know how to merge this code with the script in th ehead that's why I left it like that, can you please suggest a way?
This is the script from <head>


<script language="JavaScript">
<!-- hide this script from older browsers
      function validate(form)  {
            if (form.companyAddress.value=="")  {
            alert("Please fill in the address");
            form.companyAddress.focus();
            }

else if (form.companyPhoneNo.value=="")  {
            alert("Please fill in the phone number");
            form.companyPhoneNo.focus();
            }
...
else {
                  form.submit();
            }


anna_suchodolskaAsked:
Who is Participating?
 
copyPasteGhostConnect With a Mentor Commented:
well you have to think of it this way... if it's on keypress that means that they are inserting something...so it's not going to be empty..
the way I suggested before will work :)
try it
can I help you will anything else?
Ghost
0
 
copyPasteGhostCommented:
use java script like this...
<script language="JavaScript">
<!-- hide this script from older browsers
function validate() {
  if(document.form.companyAddress.value == "") {
    window.alert("Please enter the company Address.");
    document.form.companyAddress.focus();
    return false;
  }

  if(document.form.companyPhoneNo.value == "") {
    window.alert("Please enter the company Phone number.");
    document.form.companyPhoneNo.focus();
    return false;
  }
}
-->
</script>
<form action="nextPage.jsp" method="post" name="form" onSubmit="return checkForm();">
<table>
<tr>
<td align="left"><b>Company phone number</b></td>
<td align=top><input type="text" name="companyPhoneNo" size="20"
     value="<jsp:getProperty name ="customer" property="companyPhoneNo"/>"
     onKeypress="if (event.keyCode < 45 || event.keyCode > 57) event.returnValue = false;">
</td>
</tr>
</table>
<input type="submit" name="submitForm" value="Save Details" />
<input type="reset" name="reset" value="Reset" />
</form>

hope that helps,
Ghost
0
 
anna_suchodolskaAuthor Commented:
why I should not use else if and this bit?

else {
               form.submit();
          }

function validate(form)  { is th ename of the function I am using.  I am not using document. and window. does it make any difference?


I tried the code and pop up boxes do not pop up at all...
What can I do?

Thanks
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
anna_suchodolskaAuthor Commented:
or at  least how can I use this line
onKeypress="if (event.keyCode < 45 || event.keyCode > 57) event.returnValue = false;">
and event objet to check for "", i.e. empty string?

Can I at all?
0
 
anna_suchodolskaAuthor Commented:
or how can I place this code (event.keyCode < 45 || event.keyCode > 57) into the head script?
0
 
copyPasteGhostCommented:
if you use the code that I showed
it will work
Ghost
0
 
copyPasteGhostCommented:
if you use that code it doesn't work? the onKeyPress event... that logically looks like the right place to add that line..
Ghost
0
 
anna_suchodolskaAuthor Commented:
Help me please, I must be making some silly mistake which I can't see

<script language="JavaScript">
<!-- hide this script from older browsers
      function validate()  {
            if (document.form.companyAddress.value=="")  {
                  window.alert("Please fill in the address");
                  document.form.companyAddress.focus();
                  return false;
            }
             if (document.form.companyPhoneNo.value=="")  {
                  window.alert("Please fill in the phone number");
                  document.form.companyPhoneNo.focus();
                  return false;
            }
             if (document.form.companyFaxNo.value=="")  {
                  window.alert("Please fill in the fax number");
                  document.form.companyFaxNo.focus();
                  return false;
            }
             if (document.form.mainContactName.value=="")  {
                  window.alert("Please fill in the main contact names");
                  document.form.mainContactName.focus();
                  return false;
            }
             if (document.form.mainContactEmail.value=="")  {
                  window.alert("Please fill in the email address of the main contact. Make sure that the format is correct");
                  document.form.mainContactEmail.focus();
                  return false;
            }
             if (document.form.mainContactPhoneNo.value=="")  {
                  window.alert("Please fill in a phone number of the main contact");
                  document.form.mainContactPhoneNo.focus();
                  return false;
            }
            if (document.form.creditCardNumber.value=="" ||
                  form.creditCardNumber.value.length < 16 || form.creditCardNumber.value.length > 16 )  {
                  window.alert("Please fill in a valid card number");
                  document.form.creditCardNumber.focus();
                  return false;
            }
             if (document.form.creditCardExpirationDate.value=="")  {
                  window.alert("Please fill in expiry date");
                  document.form.creditCardExpirationDate.focus();
                  return false;
            }
             if (document.form.creditCardType.value=="")  {
                  window.alert("Please fill in credit card type");
                  document.form.creditCardType.focus();
                  return false;
            }
            if  (document.form.creditCardHolder.value=="")  {
                  window.alert("Please fill in name of the card holder");
                  document.form.creditCardHolder.focus();
                  return false;
            }
      }
// end hiding -->
</script>
</head>
<body>
<form name="tableform" action="/TaxiGlobe/DisplayCustomer.jsp" method="post"
            onSubmit="return checkForm();">
...
<input type="submit" name="submitForm" value="Save Details" />
            <input type="reset" name="reset" value="Reset" />

</center>                        
</form>

Is there anything wrong?
0
 
copyPasteGhostCommented:
that doesn't work?
what's happening?
Ghost
0
 
anna_suchodolskaAuthor Commented:
Don't know... I will try to sort it once I work out the problem below.

A little question out of context:
Now I am working with set all properties and redirect like last time and some strange things are happening.  The code is a bit different, but I will work it out.  Just tell me if you can, what may be causing:

Error: Attempt to clear a buffer that's already been flushed  :(  

:)
0
 
anna_suchodolskaAuthor Commented:
no worries, I think there may be many reasons...
0
 
anna_suchodolskaAuthor Commented:
Ghost, do you know if I can add here (event.keyCode < 45 || event.keyCode > 57) validation for empty string, or does it have to be done separately like you suggested?
0
 
copyPasteGhostCommented:
I think you can add it at the same place....you mean this right?

if (document.form.companyPhoneNo.value=="")  {
               window.alert("Please fill in the company Phone Number");
               document.form.companyPhoneNo.focus();
               return false;
          }
<input type="text" name="companyPhoneNo" size="20"
     value="<jsp:getProperty name ='customer' property='companyPhoneNo'/>"
     onKeypress="if (event.keyCode < 45 || event.keyCode > 57) return false;">

I think that's how you should do it...I'm not a javascript expert so I don't really know but I think that's the way.

Ghost
0
 
anna_suchodolskaAuthor Commented:
no, no  I mean something like this

onKeypress="if (!(companyPhoneNo.value=="") || event.keyCode < 45 || event.keyCode > 57) return false;">
 ??
0
 
anna_suchodolskaAuthor Commented:
sorry, it doesn't make any sense, the form could be submitted anyway, ignore it please
0
 
anna_suchodolskaAuthor Commented:
I am sure it works, but...
old story:

1)   the button on the page before this one was called submitForm and request.getParameter("submitForm") != null appearantly was reading that one

2)
<%
   if (request.getParameter("submitPage") != null) {
      customer.clear();
   }
%>

<%       
      String posted = request.getParameter("submitPage");
      
      if(posted == null)
      {
%>            
<input type="submit" name="submitPage" value="Save Details" onClick="validate(this.form)">
...

3) the code which I was using, I mean, javascript works completely fine on simple jsp or html but not with 2)

Can you suggest anything and tell me if it is not clear, it must be hard for you guesing what I mean :)
0
 
anna_suchodolskaAuthor Commented:
with this code window pops out asking to fill up the missed text field but when I clikk ok it goes to the next page...
0
 
anna_suchodolskaAuthor Commented:
last question before this javascript drives me mad

I think that there is some conflict between javascript button and the form button, and I can't figure out how to sort it

how can I implement input fields validation in jsp
do I need to code it in a servlet?

0
 
anna_suchodolskaAuthor Commented:
I found the problem, my form name was different than this in the script

thanks for your help anyway!
cheers
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.