validation - Javascript to validate only Numeric after predefined text in a textbox -


i'm trying use javascript make textbox contains read-only text @ beginning of text box , allows editing following read-only text. need allow 10 digit numeric after readonly text , need validate numeric digits. following javascript code having readonly in textbox

var readonlylength = $('#field').val().length;  $('#output').text(readonlylength);enter code here $('#field').on('keypress, keydown', function(event) { var $field = $(this); $('#output').text(event.which + '-' + this.selectionstart); if ((event.which != 37 && (event.which != 39))     && ((this.selectionstart < readonlylength)     || ((this.selectionstart == readonlylength) && (event.which == 8)))) {     return false; } });        

if looking solution script here plain js , regexp:

var fixedtext = function(textbox, label) {    var num = textbox.value.replace(/\d/g, ''); //non numeric    num = num.substr(0, 10); //max 10 digits    textbox.value = label + num;  };
<input type="text" onkeyup="fixedtext(this, 'postal code: ')" autofocus='' value="postal code: " />  <input type="text" onkeyup="fixedtext(this, 'contact no: ')" value='contact no: ' />

with simple label ,input , script:

var numeric = function(textbox) {    var num = textbox.value.replace(/\d/g, '');    num = num.substr(0, 10);    textbox.value = num;  };
label.partial,  input.partial {    border: 1px ridge grey;    margin-bottom: 3px;  }  label.partial {    border-right: none;    cursor: text;  }  input.partial {    border-left: none;    margin-left: -4px;    top: -1px;    position: relative;  }
<label for="postalcode" class='partial'>postal code&nbsp;</label>  <input type="text" onkeyup="numeric(this)" autofocus='' id='postalcode' class='partial' />  <br/>  <label for="contactno" class='partial'>contact no&nbsp;</label>  <input type="text" onkeyup="numeric(this)" id='contactno' class='partial' />


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -