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

thumbnail
5409340 modifierades för 15 År sedan. Junior Member Inlägg: 53 Anslutningsdatum: 2010-07-26 Senaste inlägg
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 modifierades för 15 År sedan. New Member Inlägg: 19 Anslutningsdatum: 2008-02-26 Senaste inlägg
You can use struts validator or your own javascript to validate the input value.
thumbnail
4239319 modifierades för 15 År sedan. Regular Member Inlägg: 159 Anslutningsdatum: 2009-11-06 Senaste inlägg
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 modifierades för 15 År sedan. Liferay Legend Inlägg: 1744 Anslutningsdatum: 2008-11-06 Senaste inlägg
You can use the followin Liferay API too

Validator.isNumber("value");

It will return true or false

Regards,
Sandeep
thumbnail
1284716 modifierades för 15 År sedan. Regular Member Inlägg: 124 Anslutningsdatum: 2008-09-09 Senaste inlägg
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 modifierades för 15 År sedan. Liferay Master Inlägg: 659 Anslutningsdatum: 2010-06-15 Senaste inlägg
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...