Adding form validation for numeric values

Reply

Join Date: Oct 2009
Posts: 10
Reputation: vishalanuj is an unknown quantity at this point 
Solved Threads: 0
vishalanuj vishalanuj is offline Offline
Newbie Poster

Adding form validation for numeric values

 
0
  #1
Oct 13th, 2009
  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>
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 10
Reputation: vishalanuj is an unknown quantity at this point 
Solved Threads: 0
vishalanuj vishalanuj is offline Offline
Newbie Poster
 
0
  #2
Oct 13th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,182
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 481
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer
 
0
  #3
Oct 13th, 2009
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.
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 10
Reputation: vishalanuj is an unknown quantity at this point 
Solved Threads: 0
vishalanuj vishalanuj is offline Offline
Newbie Poster
 
0
  #4
Oct 13th, 2009
This javascript is giving an error

  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)
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 6
Reputation: ddempsey96 is an unknown quantity at this point 
Solved Threads: 0
ddempsey96 ddempsey96 is offline Offline
Newbie Poster
 
0
  #5
Oct 13th, 2009
Does [0-9] need to be in quotes?
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,608
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 463
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human
 
1
  #6
Oct 13th, 2009
> 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:
  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.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC