Jump to content
php.lv forumi

Input laukā var ievadīt tikai ciparus (0-9)


senters

Recommended Posts

Tātad ir forma, ir inputs visparastākais. Kā uzrakstīt, lai lietotājs no klavieres var vadīt tikai ciparus 0-9, neko citu nevar ievadīt.

Šoreiz nevajag tā ka vada visu, tad validē un bļauj par erroriem. Kāds ir visvienkāršākais veids kā šo panākt?

Edited by senters
Link to comment
Share on other sites

Kaut kā apmēram šitā :

 

(function(){

function in_array( what, where ){
	var a=false;
	for( var i=0; i<where.length; i++ ){
		if( what == where[i] ){
			a=true;
			break;
		}
	}
	return a;
}


function validate( e ) {
	var e = e || window.event;
	var keycode = e.keyCode || e.which;
	key = String.fromCharCode( keycode );
	var regex = /[0-9]|\./;
	if( !regex.test(key) && ( !in_array( keycode ,  [ 8 , 9, 35, 36, 37, 38, 39, 40, 45, 46]  ))) {
		e.returnValue = false;
		e.preventDefault();
	}
}

docuemnt.getElementById( 'digital' ).onkeypress = validate;

})();

 

Neesmu notestējis.

Link to comment
Share on other sites

<script type='text/javascript'>
function isNumeric(elem, helperMsg){
var numericExpression = /^[0-9]+$/;
if(elem.value.match(numericExpression)){
	return true;
}else{
	alert(helperMsg);
	elem.focus();
	return false;
}
}
</script>
<form>
Numbers Only: <input type='text' id='numbers'/>
<input type='button' 
onclick="isNumeric(document.getElementById('numbers'), 'Tikai ciparus')"
value='Check Field' />
</form>

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...