Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what should be the RegExp for app version validation?

I am stuck in one RegExp where I need to validate app version for app store and play-store. I have tried several RegExp but none of them is useful for me. Here are the example that pass the test

App version up-to 2-3 decimal point

1.0 // pass

1.0.0 // pass

1.0.0.0 // fail

a.0 // fail

1 // pass

I found one RegExp [0-9]+\.[0-9]+\.[0-9]+\.[0-9]+ but this will only be valid when I have enter 4 decimal points. I don't know how to modify this.

Please help.

like image 214
Ionic Avatar asked Dec 02 '22 10:12

Ionic


2 Answers

you have mentioned up to 2-3 decimal then the RegExp must be this

^(\d+\.)?(\d+\.)?(\d+\.)?(\*|\d+)?$
like image 54
Kishan Oza Avatar answered Dec 21 '22 22:12

Kishan Oza


You can try the following regex

let reg = /^[0-9]((\.)[0-9]){0,2}$/

console.log(reg.test('1.0')) //true
console.log(reg.test('1.1.0')) //true
console.log(reg.test('1')) //true

console.log(reg.test('1.')) //false
console.log(reg.test('1.a')) //false
console.log(reg.test('1.1.1.1')) //false
like image 43
Maheer Ali Avatar answered Dec 22 '22 00:12

Maheer Ali