<%-- 
    Document   : AddCity
    Created on : Oct 6, 2009, 9:14:52 PM
    Author     : Vishal
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body onload="ShowMessages();">
	<html:form action="AddPage.do">
	
	<p><a href="HomePage.do">HomePage</a></p>
		Cityname:
		<input type="text" name="cn" value="">
		PinCode:
		<input type="text" name="pc" value=""><br>
		<input type="button" name="Submit" value="Submit1" onClick="checkpincode();onSubmit()">
		<div id="message" style="position:relative;left:0;top:0;display:none"><html:errors/></div>
	</html:form>
	</body>
</html>

<script>
function onSubmit()
{
	document.forms[0].action="AddPageSuccess.do";
	document.forms[0].submit();
}
function ShowMessages()
{
	var msg= document.getElementById("message").innerHTML;
	if(msg!=null && msg.length >0)
	{
		msg=msg.replace(/<BR>/g,"\n ");
		msg=msg.replace(/<br>/g,"\n ");
		alert(msg);
		document.getElementById("message").innerHTML="";
	}
}
</script>

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.

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.

This javascript is giving an error

function checkpincode(elem,helperMsg)
{
	var numericExpression=[0-9];
	if(elem.value.match(numericExpression))
	{
		return true;
	}
	else
	{
		alert(helperMsg);
		elem.focus();
		return false;
	}
	
}

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.

Does [0-9] need to be in quotes?

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

// This restricts the pin code to only digits with at least one
// digit required.
/^[0-9]+$/.test(txtField.value)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.