senters Posted August 26, 2010 Report Share Posted August 26, 2010 (edited) 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 August 26, 2010 by senters Quote Link to comment Share on other sites More sharing options...
zarins Posted August 26, 2010 Report Share Posted August 26, 2010 Javascript Šeit par ir piemērs. Noteikti var arī vienkāršāku to kodu, bet ieskatam būs ok. Quote Link to comment Share on other sites More sharing options...
php newbie Posted August 26, 2010 Report Share Posted August 26, 2010 Šoreiz nevajag tā ka vada visu, tad validē un bļauj par erroriem. bet servera puse vienalga ir jāparbauda. Quote Link to comment Share on other sites More sharing options...
codez Posted August 26, 2010 Report Share Posted August 26, 2010 ar jquery: $('.numbersonly').keypress(function(e){ if ((e.charCode < '48') || (e.charCode > '57')) { e.preventDefault(); } }); Quote Link to comment Share on other sites More sharing options...
Леший Posted August 26, 2010 Report Share Posted August 26, 2010 Derētu arī pārtvert paste eventu. $(".numbersonly").bind('paste', function(e){ setTimeout(čekošanas_funkcija, 10, $(e.currentTarget)) }); Timeout ir vajadzīgs, lai čekošanas_funkcija nostrādātu pēc iepeistošanas, nevis pirms. Quote Link to comment Share on other sites More sharing options...
mefisto Posted August 26, 2010 Report Share Posted August 26, 2010 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. Quote Link to comment Share on other sites More sharing options...
indoom Posted August 26, 2010 Report Share Posted August 26, 2010 var arī šitā document.getElementById('numbersonly').onkeyup = function(){ this.value = this.value.replace(/[^\d]/,''); }; Quote Link to comment Share on other sites More sharing options...
senters Posted August 26, 2010 Author Report Share Posted August 26, 2010 pagaidām paldies par atbildēm, vakarā jāapskata šie varianti Quote Link to comment Share on other sites More sharing options...
kalabox Posted August 27, 2010 Report Share Posted August 27, 2010 <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> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.