Paul
Paul

Reputation: 4428

React native js regex number with comma, without a point, space and a line

I have a numeric type input field on an app on react-native.

This field must accept comma numbers only.

So I used: keyboardType="numeric"

But in this case it is still possible to use characters such as point, space and line (-). In addition to any errors in entering incorrect number data.

The correct format should be this: nnn,ddd

Can you give me a hand?

let a = [
  "0",//0
  "0,5",//0,5
  "0,,5",//0,5
  "0.5",//0,5
  "0..5",//0,5
  ".0.5",//0,5
  ",0,5",//0,5
  "0.-5,",//0,5
  "0,-5.",//0,5
  "0,5-..",//0,5
  "0,,,- 5,,",//0,5
  "0...5-  ",//0,5,
  "0,5,"//0.5,
];

a.map(el=> {
  const e = el
  .replaceAll('.', ',')
  .replace(/[^0-9,]/g, '')
  console.log(e)
});

Edit:

let a = [
  "0",//0
  "0,5",//0,5
  "0,,5",//0,5
  "0.5",//0,5
  "0..5",//0,5
  ".0.5",//0,5
  ",0,5",//0,5
  "0.-5,",//0,5
  "0,-5.",//0,5
  "0,5-..",//0,5
  "0,,,- 5,,",//0,5
  "0...5-  ",//0,5,
  
  "00,5",//0,5
  "00,05",//0,05
  "05,512",//5,51
  "00,5123",//0,51
  ",05",//0,05
  "00,,5",//0,5
  "21,"//21
];

Upvotes: 1

Views: 1577

Answers (3)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626806

You can use

let a = [
  "0",//0
  "0,5",//0,5
  "0,,5",//0,5
  "0.5",//0,5
  "0..5",//0,5
  ".0.5",//0,5
  ",0,5",//0,5
  "0.-5,",//0,5
  "0,-5.",//0,5
  "0,5-..",//0,5
  "0,,,- 5,,",//0,5
  "0...5-  ",//0,5,
  
  "00,5",//0,5
  "00,05",//0,05
  "05,512",//5,51
  "00,5123",//0,51
  ",05",//0,05
  "00,,5",//0,5
  "21,"//21
];

a.map(el=> {
  const e = el
  .replace(/^[.,]+(?=[^.,]*$)/, '0,') // Add 0 before the first . or ,  if they are single
  .replace(/^\D+|\D+$|[^0-9,.]/g, '') // remove all non-digits on both sides and non-digits except . and , in the middle
  .replace(/[,.]+/g, ',') // all commas/dots to commas
  .replace(/^0+(?=\d)|,(?=[^,]*,)|(,\d{2})\d+$/g, '$1') // remove leading zeros, all commas but last, and all fractional digits after 2nd
  console.log(el,'=>',e);
});

Here,

  • .replace(/^[.,]+(?=[^.,]*$)/, '0,') - adds 0 before the first . or , if they are the only comma/dot(s) in the string
  • .replace(/^\D+|\D+$|[^0-9,.]/g, '') - removes all non-digits on both sides of the string and non-digits except . and , in the middle of the string
  • .replace(/[,.]+/g, ',') - replaces all commas/dots with a comma
  • .replace(/^0+(?=\d)|,(?=[^,]*,)|(,\d{2})\d+$/g, '$1') - removes leading zeros, all commas but last, and all fractional digits after the second fractional digit.

In your code, you should allow a comma at the end:

price: price
    .replace(/(,[^,]*)[,.]$/, '$1') // remove trailing comma/dot if there is a comma already
    .replace(/^[.,]+(?=[^.,]*$)/, '0,') // Add 0 before the first . or ,  if they are single
    .replace(/[^0-9,.]/g, '') // remove all non-digits on both sides and non-digits except . and , in the middle
    .replace(/[,.]+/g, ',') // all commas/dots to commas
    .replace(/^0+(?=\d)|,(?=[^,]*,)|(,\d{2})\d+$/g, '$1') // remove leading zeros, all commas but last, and all fractional digits after 2nd

Upvotes: 2

Kinglish
Kinglish

Reputation: 23654

Here's a bit of a hybrid string manipulation mixed with regex. Turned into a bit of a monster with the edited conditions, but here goes:

a.map(el => {
  const e = el
  /* normalize decimals */
    .replaceAll('.', ',')       
  /* break into array */
    .split(",")
  /* add a starting zero  */     
    .map((e,i) => i === 0 ? "0"+e : e)       
  /* get rid of empties (extra commas) */
    .filter(e => e)       
  /* only work with the last 2 */
    .splice(-2)  
  /* remove non-numbers */                                           
    .map(e => e.replace(/[^0-9.]/g, ''))                    
  /* trim index 0 & 1 */
    .map((e, i) => i === 0 ? (+e || "0") : e.substr(0, 2)) 
  /* reassemble */ 
    .join(",");                                             
  console.log(el, '=>', e)
});

let a = [
  "0", //0
  "0,5", //0,5
  "0,,5", //0,5
  "0.5", //0,5
  "0..5", //0,5
  ".0.5", //0,5
  ",0,5", //0,5
  "0.-5,", //0,5
  "0,-5.", //0,5
  "0,5-..", //0,5
  "0,,,- 5,,", //0,5
  "0...5-  ", //0,5,

  "00,5", //0,5
  "00,05", //0,05
  "05,512", //5,51
  "00,5123", //0,51
  ",05", //0,05
  "00,,5", //0,5
  "21," //21
];


a.map(el => {
  const e = el.replaceAll('.', ',').split(",").map((e,i) => i === 0 ? "0"+e : e).filter(e => e).splice(-2).map(e => e.replace(/[^0-9.]/g, '')).map((e, i) => i === 0 ? (+e || "0") : e.substr(0, 2)).join(","); 
  console.log(el, '=>', e)
});

Upvotes: 2

uingtea
uingtea

Reputation: 6524

self explained

let a = [
  "0",//0
  "0,5",//0,5
  "0,,5",//0,5
  "0.5",//0,5
  "0..5",//0,5
  ".0.5",//0,5
  ",0,5",//0,5
  "0.-5,",//0,5
  "0,-5.",//0,5
  "0,5-..",//0,5
  "0,,,- 5,,",//0,5
  "0...5-  ",//0,5,
  "0,5,"//0.5
];

a.map(el=> {
  const e = el
  .replace(/\D+/g, ',') // replace non digit with comma
  .replace(/^,|,$/g, '') // trim the comma
  console.log(e)
});

Upvotes: 1

Related Questions