logo
down
shadow

Format text input value with javascript


Format text input value with javascript

By : Frédéric
Date : November 21 2020, 04:01 AM
around this issue This is my approach to formating the string, it might not be the most optimal but it does the trick quite nicely.
code :
let inputString = '01234567890';
let splitIndex = 6;

function format (value) {
  let firstPart = splitString(value.substring(0, splitIndex), 2).join('.');
  let secondPart = splitString(value.substring(splitIndex), 3).join('.');
  
  return [firstPart, secondPart].join('-');
}

function splitString (str, n) {
  let arr = [];
  var len;

  for(let i = 0, len = str.length; i < len; i += n) {
    arr.push(str.substr(i, n))
  }

  return arr;
}

let formatedInput = format(inputString);

console.log(formatedInput);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


Share : facebook icon twitter icon
Converting date in text input string into other format javascript

Converting date in text input string into other format javascript


By : Andrew Fawcett
Date : March 29 2020, 07:55 AM
wish of those help The date input I have is for e.g. , There's no need to use a library:
code :
var startDate = "January 16, 2013";
var d = new Date(startDate);
var month = d.getMonth() + 1;
var day = d.getDate();
var year = d.getFullYear();

alert(month + "-" + day + "-" + year);
var startDate = "ApRiL 16, 2013";
var noCommas = startDate.replace(/\,/g,'');
var withDashes = noCommas.replace(/ /g,"-");

function monthsToNumbers(date)
{
  var months = ["january","february","march","april","may","june","july","august","september","october","november","december"];
  var year;

  date = date.toLowerCase();

  for(var i = 0; i < months.length; i++)
  {
    if(date.indexOf(months[i]) != -1)
    {
      if(i+1 < 10)
        date = date.replace(months[i],"0" + (i+1));
      else
        date = date.replace(months[i],i+1);
    }
  }

  year = date.match(/\d{4}/);
  date = date.replace(year, "");
  date = year + "-" + date;
  date = date.substring(0,date.length-1);

  return date;
}

var d = new Date(monthsToNumbers(withDashes));
var month = d.getMonth() + 1;
var day = d.getDate();
var year = d.getFullYear();

alert(d);
Is there a javascript module to format user text input into proper "english" format?

Is there a javascript module to format user text input into proper "english" format?


By : Rubeus
Date : March 29 2020, 07:55 AM
it should still fix some issue I'm not exactly clear what you want the intended output to be? ("add in newlines and stuff"), but have you looked at markdown?
It's a light-weight markup language lets you write mostly in plain text and get formatted html as an output. All the markup is supposed to be plain-text readable (for example, bold is done with asterisks). In fact, StackOverflow comments/answers/questions are written in a markdown dialect! And you'll definitely be able to find a markdown converter for whatever language you want. Here's a js markdown parser.
Format date into yyyy-mm-dd in text box where the only input is mm-dd JavaScript

Format date into yyyy-mm-dd in text box where the only input is mm-dd JavaScript


By : Idham Zali
Date : March 29 2020, 07:55 AM
it should still fix some issue Using yyyy-mm-dd for input is usually inconvenient for users, most prefer to type d-m-y except for some places where m-d-y might be preferred.
Using d-m-y also helps as it's easy to tell for partly written dates what the values are. E.g. 16/5 is 16 May in the current year, but if the format is y-m-d then I don't know if 5-m should be 2005 or not.
code :
function setDate() {

  // Padding helper
  function z(n) {
    return (n < 10 ? '0' : '') + n
  }

  var today = new Date();
  var b = this.value.split(/\D/);
  var year = today.getFullYear();

  // If only a 2 part date was entered, create full date and
  // assume mm-dd. Otherwise do nothing
  if (b.length < 3) {
    year += b[0] <= today.getMonth()? 1 : 0;
    this.value = year + '-' + z(+b[0]) + '-' + z(+b[1])

  // For other values, make sure the separator is '-'.
  } else {
    this.value = b.map(z).join('-');
  }
}

window.onload = function() {
  var el = document.getElementById('aDate');
  if (el) {
    el.addEventListener('blur', setDate, false);
  }
};
Enter a Date:<input id="aDate" name="aDate" placeholder="yyyy-mm-dd or mm-dd">
Format input text with regular expressions in javascript

Format input text with regular expressions in javascript


By : user1825466
Date : March 29 2020, 07:55 AM
Does that help You can check the length and use indexOf to find if this string contains a '-'
code :
function maskinput(e) {
  if (e.target.value.length > 7 && e.target.value.indexOf('-') === -1) {
    let substring1 = e.target.value.slice(0, 7),
      substring2 = e.target.value.slice(7, e.target.value.length);
    e.target.value = substring1 + '-' + substring2;
  }
}
<input type='text' onkeyup='maskinput(event)'>
Javascript text input on input text change format to two decimal places

Javascript text input on input text change format to two decimal places


By : user3111315
Date : March 29 2020, 07:55 AM
hop of those help? Hi I have the below code which sanitises text entered in input text. , You can change your second regex to this
code :
.replace(/(\.\d{1,2}).*/g, "$1") // remove multiple points.
(\.\d{1,2}).*/
let formattedText = (text) => text
  .replace(/[^0-9.]/g, "")
  .replace(/\.{2,}/g,'.')
  .replace(/^0*([^0]\d*\.\d{1,2}).*/g, "$1")
  
  
console.log(formattedText('abcb123.25252.235252abdbch'))
console.log(formattedText('abcb123.2.2gvsgvs'))
console.log(formattedText('abcbcbbc123'))
console.log(formattedText('123.avsvs.123'))
console.log(formattedText('avsvs.1234'))
console.log(formattedText('avsvs1234'))
  .replace(/\.{2,}/g,'.')
.replace(/^0*([^0]\d*\.\d{1,2}).*/g, "$1")
Related Posts Related Posts :
  • How do i determine whether a particular image has already been cached using angular 6
  • How to read JSON string sent from the frontend
  • Changing background image with time
  • How to define if component nested to itself in Angular 6?
  • Building a board game using canvas + css animations
  • dropdown flexibility?
  • How "d3.translateExtent" coordinate works?
  • Get the element of an array that has an index given by a variable in JavaScript
  • Replace(), replacing a value not specified
  • javascript native equivalent to .each & .attr
  • Fabric getPointer method not working in Firefox
  • JS scripts not working for divs loaded with jQuery's .load()
  • Use xhr code error on switch/case (javascript)
  • ParseFloat return string instead of number in Javascript
  • How to add a custom script into React app component?
  • Show data while it's being sent to the backend
  • selecting a specific class with .parents()?
  • Get value of a selected option in an label with Javascript
  • How Do I get an HOC wrapped component function from enzyme instance when mounting?
  • i want to update the flashdata on every ajax call
  • Cannot find a tag with JSsoup even though the tag exists in Node JS
  • Exporting the RouterModule?
  • Remove the default checked value in radio button and replace it with the another radio button that is click
  • Switch to test for id and perform action based on class
  • JavaScript Random Number Generation: Unique 500 Integers in Space 10^6: Getting Collisions
  • Display files in directory using PHP and jQuery
  • Knockout JS: adding forms in foreach bindings
  • HTML5 Canvas positioning mouse clicking broken
  • JQuery on change fired only after second change of select box
  • Unable to limit WebRTC P2P Multi-participant Receiving Bandwidth
  • What is the RegEx to just return the last part of the URL
  • State Remains Undefined in React
  • removing content on website before a screen reader can read it with either JavaScript or CSS...?
  • on 'click' firing before click
  • Manipulation of an array sent by ajax to a php file, within a function of another file
  • How to use plunker or jsfiddle to run angular2 code
  • JavaScript lexical environments as objects
  • Iteratively position spheres with DOM pixels using three.js and React
  • React trying to access file at a wrong path
  • AES encryption in JS equivalent of C#
  • Filter an Array based off user input string and menu dropdown items
  • Constantly read local file with JS?
  • React navbar handler with theme
  • Show changes to HTML5 canvas immediately
  • Tooltip Stays Hidden In D3 Treemap
  • Backbone - What is the best way to listen to 2 models?
  • formatting an array of objects output
  • Bind input value to specific value in object
  • Animation doesnt work when onClick event occurs (react.js)
  • Consume enum from ambient TypeScript module using @babel/preset-typescript?
  • Changing order of React components only triggers re-render after second click
  • Track the total time the HTMLAudio was playing
  • Call javascript function in PHP file and load html file after?
  • Theme Development: JavaScript not working when linked from functions.php
  • Add attributes to multiple <td> in datatables
  • Progressive loading codesnip not quite understood
  • How to format default date in javascript?
  • Caesar cipher only gets first value right
  • innerText not showing after setting it straight after creating the element
  • How to use HTML template tag with jQuery?
  • shadow
    Privacy Policy - Terms - Contact Us © bighow.org