[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Java script for input validation redirects to the next page

Posted on 2004-04-21
19
Medium Priority
?
956 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 13
  • 6
19 Comments
 
LVL 13

Expert Comment

by:copyPasteGhost
ID: 10883262
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
ID: 10891486
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
ID: 10891867
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

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

Expert Comment

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

Expert Comment

by:copyPasteGhost
ID: 10891953
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
ID: 10892320
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
ID: 10892657
that doesn't work?
what's happening?
Ghost
0
 

Author Comment

by:anna_suchodolska
ID: 10892844
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
 

Author Comment

by:anna_suchodolska
ID: 10892981
no worries, I think there may be many reasons...
0
 

Author Comment

by:anna_suchodolska
ID: 10893025
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
ID: 10893059
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
ID: 10893210
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
ID: 10893215
sorry, it doesn't make any sense, the form could be submitted anyway, ignore it please
0
 
LVL 13

Accepted Solution

by:
copyPasteGhost earned 90 total points
ID: 10893237
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
ID: 10893769
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
ID: 10893786
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
ID: 10893905
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
ID: 10895470
I found the problem, my form name was different than this in the script

thanks for your help anyway!
cheers
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What monsters are hiding in your child's room? In this article I will share with you a tech horror story that could happen to anyone, along with some tips on how you can prevent it from happening to you.
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

650 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