[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1347
  • Last Modified:

jQuery: validating integer in a target textbox

Hello everybody!

I am using:
jquery 1.4.2
jquery-ui-1.8.4

and I have the following code:
<form id="formParametros">
<div id="divParametrosDemo">
	<table class="ui-widget" style="text-align:left; width:90%">
		<tr class="ui-widget-header" style="text-align:center">
			<th colspan="3">Parámetros</th>
		</tr>
		<tr style="text-align:center">
			<th style="width:33%">Días por defecto</th>
			<th style="width:34%">Cantidad de Licencias por Usuario</th>
			<th style="width:33%">Evaluación de Sub-Contratos</th>
		</tr>
		<tr class="ui-widget-content">
			<td>
				<div id="divDescripcionDiasDemo" style="width:80%">
					<p>
						Los usuarios demo se les otorgará la licencia por una cantidad determinada de días.
						Si el usuario que autoriza no desea definir los días, el sistema utilizará una cantidad
						predeterminada. Dicha cantidad se define aquí.
					</p>
				</div>
			</td>
			<td>
				<div id="divDescripcionLicenciasDemo" style="width:80%">
					<p>
						Los usuarios demo se les otorgará la licencia por una cantidad determinada de días.
						Si el usuario que autoriza no desea definir los días, el sistema utilizará una cantidad
						predeterminada. Dicha cantidad se define aquí.
					</p>
				</div>
			</td>
			<td>
				<div id="divDescripcionEvaDemo" style="width:80%">
					<p>
						Los usuarios demo se les otorgará la licencia por una cantidad determinada de días.
						Si el usuario que autoriza no desea definir los días, el sistema utilizará una cantidad
						predeterminada. Dicha cantidad se define aquí.
					</p>
				</div>
			</td>
		</tr>
		<tr class="ui-widget-content">
			<td>
				<table>
					<tr>
						<td>Días:</td>
					</tr>
					<tr>
						<td><input type="text" id="txtDiasDemo" maxlength="2" /></td>
					</tr>
				</table>
			 </td>
			<td>
				<table>
					<tr>
						<td>Licencias:</td>
					</tr>
					<tr>
						<td><input type="text" id="txtLicenciasDemo" maxlength="2" /></td>
					</tr>
				</table>
			</td>  
			<td>
				<table>
					<tr>
						<td>¿Permitir?</td>
						<td><input type="checkbox" id="chkEvaDemo" /></td>
					</tr>
					<tr>
						<td colspan="2">Días:</td>
					</tr>
					<tr>
						<td colspan="2"><input type="text" id="txtDiasEvaDemo" maxlength="2" /></td>
					</tr>
				</table>
			</td>   
		</tr>
		<tr>
			<td colspan="3" id="sumarioParametrosDemo" class="ui-state-highlight" style="visibility:hidden; text-align:center">
				&nbsp;
			</td>
		</tr>
		<tr>
			<td colspan="3" style="text-align:center" class="ui-widget-header">
				<button id="btnGuardarParametrosDemo" style="font-size:small">Guardar</button>
			</td>
		</tr>
	</table>
</div>
</form>

Open in new window


which makes the next form:
Form
So, let's go to the problem.

There are 3 text boxes in the form, I need to validate on the click event of the button that the value of those text boexes is an integer.
I know jQuery provides a validation method which should be easy to use. I've been searching in the jQuery api, with no luck (jQuery validate() API) How can I do it?
0
karelog
Asked:
karelog
  • 3
  • 2
1 Solution
 
ansudhindraCommented:
hi, I dont know much about JQuery UI, but you can do it in normal javascript, onKeyPress event on textbox like

<input type="text" name="" onkeypress="return allownumbers();">

function allownumbers(event){
  var thekey=event.keyCode;
  if(thekey > 46 && thekey < 58) return true
return false;
}

this will allow only integers into the tex box.
0
 
karelogAuthor Commented:
nice code ansudhindra!

I know I can use normal javascript, is just that I would like to learn how to use the validate() method in jQuery because it has a lot of potential!
0
 
Gurvinder Pal SinghCommented:
<<because it has a lot of potential!>>
lolz...check these
http://docs.jquery.com/Plugins/Validation/Methods/number (see demo)

http://www.kunalbabre.com/jQueryLibrary/index.php (do view source)

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
karelogAuthor Commented:
Ok, I already looked at those examples!

now I can't understand where message comes out!
I want to re-write the error message to another and I can't D:
0
 
karelogAuthor Commented:
thanks ^^
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now