www.webdeveloper.com
Results 1 to 7 of 7

Thread: String validation

  1. #1
    Join Date
    Oct 2004
    Posts
    3

    String validation

    Hi,

    I'm a newbie in javascript. I hope somebody will help me.
    For example I have 2 textboxes and 1 button. I want to make a validation that if the 2 textboxes is empty when the user click the button then it will show an alert box. How to do that?

    here is for the textboxes:
    String description = request.getParameter("description");
    String quantity = request.getParameter("quantity");

    if (description == null || quantity == null){
    %>
    <script>
    alert ('Please enter the necessary information')
    history.go(-1);
    </script>
    <%
    }
    Wat's wrong with the above code? any sample code? Thanks

  2. #2
    Join Date
    Jul 2004
    Location
    Singapore, SG
    Posts
    654
    Try validating against empty strings instead of null values

  3. #3
    Join Date
    Jul 2003
    Location
    Boston Area, Massachusetts
    Posts
    3,489
    That isn't JavaScript, but rather JSP. Moving to the Java forum; you'll get more help over there. Please post in the correct forum next time.

  4. #4
    Join Date
    Oct 2004
    Posts
    3
    Still can not.... any ideas?

  5. #5
    Join Date
    Nov 2003
    Location
    Jerryville, Tejas
    Posts
    11,715
    Being the MVC Nazi that I am, I'd say your MAIN problem is that you're processing that form with a JSP instead of a servlet.

    Javanoobie is right but for tests like that I prefer:
    Code:
    if (description != null && !description.equals("")
         && quantity != null && !quantity.equals("")) { %>
    <p>Do HTML for good stuff</p>
    <% } else { %>
    <p>Do HTML for bad stuff</p>
    <% } %>

  6. #6
    Join Date
    Oct 2004
    Posts
    3
    Hi, thanks.. but I prefer doing the validation by using javascript since it is faster but I don't know how... any sample code?

    <form name="test" method="post" action="request_action1.jsp?action=addrequest">
    <input name="description" type="text" size="75">
    function validateform()
    {
    var myForm = document.test;

    if (myForm.description.length == 0)
    {
    alert("Please enter the item description");
    history.go(-1);
    }
    }

    but the alert can not be shown... can help me resolve the problem?

  7. #7
    Join Date
    Jul 2004
    Location
    Singapore, SG
    Posts
    654
    myForm.description.value.length

    Note: There are plenty of validation codes if you do a search within the Javascript forum.
    Last edited by javaNoobie; 10-20-2004 at 03:51 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles