RE: How to resrict user to give only integer value?

thumbnail
5409340, modified 15 Years ago. Junior Member Posts: 53 Join Date: 7/26/10 Recent Posts
Hi to All,
i need a functionality that user has to give only integer in a form field
please tell me if you know

Thanks & Regards
Ramesh K
515365, modified 15 Years ago. New Member Posts: 19 Join Date: 2/26/08 Recent Posts
You can use struts validator or your own javascript to validate the input value.
thumbnail
4239319, modified 15 Years ago. Regular Member Posts: 159 Join Date: 11/6/09 Recent Posts
use jquery class ="required number"

for example:
<form>
<input type="text" name="phone" class="required number" />
</form>

Provided you should jquery.validate.js to be included
thumbnail
1545856, modified 15 Years ago. Liferay Legend Posts: 1744 Join Date: 11/6/08 Recent Posts
You can use the followin Liferay API too

Validator.isNumber("value");

It will return true or false

Regards,
Sandeep
thumbnail
1284716, modified 15 Years ago. Regular Member Posts: 124 Join Date: 9/9/08 Recent Posts
Hi,

You can use this js function also

function checkInt(e) {
var key;
var keychar;
if (window.event)
key = window.event.keyCode;
else if (e)
key = e.which;
else
return true;
keychar = String.fromCharCode(key);
keychar = keychar.toLowerCase();
// control keys
if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27) )
return true;
// numbers
else if ((("0123456789").indexOf(keychar) > -1))
return true;
else
return false;
}



you can call this function like that

<input type="text" name="<portlet:namespace />pinCode" value="" onkeypress="return checkInt(event)" />

Regards,

Masroor Khan
thumbnail
5085858, modified 15 Years ago. Liferay Master Posts: 659 Join Date: 6/15/10 Recent Posts
Ramesh K:
Hi to All,
i need a functionality that user has to give only integer in a form field
please tell me if you know

Thanks & Regards
Ramesh K


Hello Ramesh,

There are so many possible and working ways are illustrated above, but what I feel personally more comfortale is to achieve it b jquery, as:
<script src="/html/portlet/ext/Search/jquery.validate.js" language="JavaScript"> </script>
<link rel="stylesheet" type="text/css" media="screen" href="/html/portlet/ext/Search/css/screen.css">
<script type="text/javascript">

	jQuery(document).ready(function() {
		jQuery("#addform").validate({
			rules:{
				empCode:{required: true,number:true},
				firstName:"required",
				mobile:{required: true,number:true},
				technology:"required",
				email: {// compound rule 
					required: false, 
					email: true 

	        }, 
			}
		});
	});
</script>

<style type="text/css">
 #form1 { width: 500px; }
 #form1 label { width: 250px; }
 #form1 label.error, #form1 input.submit { margin-left:}
</style>

&gt; <table width="80%" align="center"> <tbody><tr> <td width="20%"><b>Emp Code <font color="red">*</font> : </b></td> <td width="60%"><input id="empCode" type="text" name="empCode"> </td></tr> <tr> <td width="20%"><b>First Name <font color="red">*</font> : </b></td> <td width="60%"><input type="text" name="firstName"> </td></tr> <tr> <td width="20%"><b>Last Name : </b></td> <td width="60%"><input type="text" name="lastName" "> </td></tr> <tr> <td width="20%"><b>E-mail : </b></td> <td width="60%"><input type="text" name="email"> </td></tr> <tr> <td width="20%"><b>Mobile <font color="red">*</font> : </b></td> <td width="60%"><input type="text" name="mobile"> </td></tr><tr> <td width="20%"><b>Technology <font color="red">*</font> : </b></td> <td width="60%"><input type="text" name="technology"> </td></tr> <tr> <td width="20%"><b>Specialization : </b></td> <td width="60%"><input type="text" name="spec"> </td></tr> <tr> <td width="20%"></td> <td width="60%"><input type="submit" name="submit" value="Add Member"> <portlet:actionurl var="backURL"> <portlet:param name="struts_action" value="/ext/Search/Search_action" /> <portlet:param name="CMD" value="back" /> </portlet:actionurl> <input type="button" name="back" onclick="submitForm(document.hrefFm, '<%=backURL%>')" value="Back"></td> </tr> </tbody></table>

In the above code, refer to the line number 8 and 32... The text box of form added in line number 32 is validated by jquery code at line number 8...

You can refer and download the library jquery-validate.js from here...

Hope this will help Ramesh...

Thanks and Regards...