質問

I am writing regex for a javascript application (Qualtrics) and can't figure out how to create regex for a phone number field that may or may not be international and may be blank. So the requirements are possible "+","0-9","-",".",""

If I can figure it out, I'd also like to create flexibility for spaces before and after the entry.

役に立ちましたか?

解決

If you just need an arbitrary number of characters including numbers, +, -, . and space (and also assuming you wish to match the entire input), then fairly simply:

^[0-9\-+.\s]*$

If your looking for something to validate placement of dots, dashes, spaces etc. and validate length, the regex to accomplish that tends to get very complicated and silly. For example, this:

^(?:(?:+?1\s*(?:[.-]\s*)?)?(?:(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})(?:\s*(?:#|x.?|ext.?|extension)\s*(\d+))?$

Seems to be a standard implementation to handle a subset of the set you wish to recognize, if I understand correctly.

An approach like that outlined in this question might be a more sensible solution.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top