944,111 Members | Top Members by Rank

Ad:
  • JSP Discussion Thread
  • Unsolved
  • Views: 2176
  • JSP RSS
Oct 13th, 2009
0

Adding form validation for numeric values

Expand Post »
JSP Syntax (Toggle Plain Text)
  1. <%--
  2. Document : AddCity
  3. Created on : Oct 6, 2009, 9:14:52 PM
  4. Author : Vishal
  5. --%>
  6.  
  7. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  9. "http://www.w3.org/TR/html4/loose.dtd">
  10.  
  11. <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
  12. <html>
  13. <head>
  14. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  15. <title>JSP Page</title>
  16. </head>
  17. <body onload="ShowMessages();">
  18. <html:form action="AddPage.do">
  19.  
  20. <p><a href="HomePage.do">HomePage</a></p>
  21. Cityname:
  22. <input type="text" name="cn" value="">
  23. PinCode:
  24. <input type="text" name="pc" value=""><br>
  25. <input type="button" name="Submit" value="Submit1" onClick="checkpincode();onSubmit()">
  26. <div id="message" style="position:relative;left:0;top:0;display:none"><html:errors/></div>
  27. </html:form>
  28. </body>
  29. </html>
  30.  
  31. <script>
  32. function onSubmit()
  33. {
  34. document.forms[0].action="AddPageSuccess.do";
  35. document.forms[0].submit();
  36. }
  37. function ShowMessages()
  38. {
  39. var msg= document.getElementById("message").innerHTML;
  40. if(msg!=null && msg.length >0)
  41. {
  42. msg=msg.replace(/<BR>/g,"\n ");
  43. msg=msg.replace(/<br>/g,"\n ");
  44. alert(msg);
  45. document.getElementById("message").innerHTML="";
  46. }
  47. }
  48. </script>
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
vishalanuj is offline Offline
10 posts
since Oct 2009
Oct 13th, 2009
0
Re: Adding form validation for numeric values
I want to add a javascript in the jsp form for the form validation of numbers in the pincode text area.I know it's easy but just not clicking .Any help would be appreciated.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
vishalanuj is offline Offline
10 posts
since Oct 2009
Oct 13th, 2009
-1
Re: Adding form validation for numeric values
onChange - the onchange event occurs when a control loses the input focus and its value has been modified since gaining focus. This attribute applies to the following elements: INPUT, SELECT, and TEXTAREA.
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 874
Code tags enforcer
peter_budo is offline Offline
6,659 posts
since Dec 2004
Oct 13th, 2009
0
Re: Adding form validation for numeric values
This javascript is giving an error

JavaScript Syntax (Toggle Plain Text)
  1. function checkpincode(elem,helperMsg)
  2. {
  3. var numericExpression=[0-9];
  4. if(elem.value.match(numericExpression))
  5. {
  6. return true;
  7. }
  8. else
  9. {
  10. alert(helperMsg);
  11. elem.focus();
  12. return false;
  13. }
  14.  
  15. }

The page is giving an error "undefined is null or not an object".Perhaps i need to provide an action path for the checkpincode() javascript.But i don't know where.Or there might be some other reason.
Last edited by peter_budo; Oct 13th, 2009 at 12:09 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
vishalanuj is offline Offline
10 posts
since Oct 2009
Oct 13th, 2009
0
Re: Adding form validation for numeric values
Does [0-9] need to be in quotes?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ddempsey96 is offline Offline
10 posts
since Oct 2009
Oct 13th, 2009
1
Re: Adding form validation for numeric values
> Does [0-9] need to be in quotes?

No, it needs to be placed inside forward slashes to be considered as a regular expression. Of course, if you place them inside quotes, you need to explicitly use the RegExp() constructor.

@vishalanuj
That regex won't work. Try something like:
javascript Syntax (Toggle Plain Text)
  1. // This restricts the pin code to only digits with at least one
  2. // digit required.
  3. /^[0-9]+$/.test(txtField.value)
Last edited by ~s.o.s~; Oct 13th, 2009 at 12:53 pm.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JSP Forum Timeline: JSP and Calendar View
Next Thread in JSP Forum Timeline: configuring Tomcat





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC