Solved

Java script for input validation redirects to the next page

Posted on 2004-04-21
19
947 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
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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 30 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
netstat -a in AIX unix 8 36
tomcat users xml 7 104
best tutorial on log4j and logging for servlets/jsp codes 1 63
Handle form fields in spring mvc controller 1 60
Lotus Notes – formerly IBM Notes – is an email client application, while IBM Domino (earlier Lotus Domino) is an email server. The client possesses a set of features that are even more advanced as compared to that of Outlook. Likewise, IBM Domino is…
Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

932 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

19 Experts available now in Live!

Get 1:1 Help Now