Solved

Java script for input validation redirects to the next page

Posted on 2004-04-21
19
946 Views
Last Modified: 2011-10-03
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();
            }


0
Comment
Question by:anna_suchodolska
  • 13
  • 6
19 Comments
 
LVL 13

Expert Comment

by:copyPasteGhost
Comment Utility
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
 

Author Comment

by:anna_suchodolska
Comment Utility
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
 

Author Comment

by:anna_suchodolska
Comment Utility
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
 

Author Comment

by:anna_suchodolska
Comment Utility
or how can I place this code (event.keyCode < 45 || event.keyCode > 57) into the head script?
0
 
LVL 13

Expert Comment

by:copyPasteGhost
Comment Utility
if you use the code that I showed
it will work
Ghost
0
 
LVL 13

Expert Comment

by:copyPasteGhost
Comment Utility
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
 

Author Comment

by:anna_suchodolska
Comment Utility
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
 
LVL 13

Expert Comment

by:copyPasteGhost
Comment Utility
that doesn't work?
what's happening?
Ghost
0
 

Author Comment

by:anna_suchodolska
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:anna_suchodolska
Comment Utility
no worries, I think there may be many reasons...
0
 

Author Comment

by:anna_suchodolska
Comment Utility
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
 
LVL 13

Expert Comment

by:copyPasteGhost
Comment Utility
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
 

Author Comment

by:anna_suchodolska
Comment Utility
no, no  I mean something like this

onKeypress="if (!(companyPhoneNo.value=="") || event.keyCode < 45 || event.keyCode > 57) return false;">
 ??
0
 

Author Comment

by:anna_suchodolska
Comment Utility
sorry, it doesn't make any sense, the form could be submitted anyway, ignore it please
0
 
LVL 13

Accepted Solution

by:
copyPasteGhost earned 30 total points
Comment Utility
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
 

Author Comment

by:anna_suchodolska
Comment Utility
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
 

Author Comment

by:anna_suchodolska
Comment Utility
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
 

Author Comment

by:anna_suchodolska
Comment Utility
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
 

Author Comment

by:anna_suchodolska
Comment Utility
I found the problem, my form name was different than this in the script

thanks for your help anyway!
cheers
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Learn to move / copy / export exchange contacts to iPhone without using any software. Also see the issues in configuration of exchange with iPhone to migrate contacts.
Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now