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

thumbnail
5409340,修改在15 年前。 Junior Member 帖子: 53 加入日期: 10-7-26 最近的帖子
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,修改在15 年前。 New Member 帖子: 19 加入日期: 08-2-26 最近的帖子
You can use struts validator or your own javascript to validate the input value.
thumbnail
4239319,修改在15 年前。 Regular Member 帖子: 159 加入日期: 09-11-6 最近的帖子
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,修改在15 年前。 Liferay Legend 帖子: 1744 加入日期: 08-11-6 最近的帖子
You can use the followin Liferay API too

Validator.isNumber("value");

It will return true or false

Regards,
Sandeep
thumbnail
1284716,修改在15 年前。 Regular Member 帖子: 124 加入日期: 08-9-9 最近的帖子
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,修改在15 年前。 Liferay Master 帖子: 659 加入日期: 10-6-15 最近的帖子
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...