Regex for a name and number dart

Regex for a name and number dart



I need to validate a form that a user provides their name and a number. I have a regex that is supposed to make sure the name contains only letters and nothing else and also for the number input i need to make sure only numbers are in the field.
The code I have looks like


validator: (value) => value.isEmpty
? 'Enter Your Name'
: RegExp(
'!@#<>?":_``~;|=-+)(*&^%1234567890')
? 'Enter a Valid Name'
: null,



can i get a regex expression that validates a name in such a way if any special character or number is inputted it becomes wrong meaning only letters are valid and another that validates a number in such a way if an alphabetical letter or any special character is inputted it becomes wrong meaning only the input of a number is valid





Looks like you wanted RegExp(r'[!@#<>?":_`~;[]\|=+)(*&^%0-9-]').hasMatch(value). You need to use a raw string literal, put - at the end and escape ] and chars, then check if there is a match with .hasMatch(value). [0123456789] = [0-9].
– Wiktor Stribiżew
Aug 24 at 21:12



RegExp(r'[!@#<>?":_`~;[]\|=+)(*&^%0-9-]').hasMatch(value)


-


]



.hasMatch(value)


[0123456789]


[0-9]





This works for the Name RegExp. Can you explain for the number one. In this one, the user is presented with the number keyboard so some characters are not important as such. I have just copied the name Regex and pasted it here but removed 0-9 in order to be able to input the numbers. Problem is, when a space is inserted, it still counts it as a value. How can i correct this.
– Kevin Robert
Aug 25 at 8:14


0-9





Add s - r'[!@#<>?":_`~;[]\|=+)(*&^%s-]'
– Wiktor Stribiżew
Aug 25 at 9:11



s


r'[!@#<>?":_`~;[]\|=+)(*&^%s-]'





Yes. This works. You can add the answer
– Kevin Robert
Aug 25 at 9:32





Just got back home, posted as an answer.
– Wiktor Stribiżew
Aug 25 at 20:39





2 Answers
2



It seems to me you want


RegExp(r'[!@#<>?":_`~;[]\|=+)(*&^%0-9-]').hasMatch(value)



Note that you need to use a raw string literal, put - at the end and escape ] and chars inside the resulting character class, then check if there is a match with .hasMatch(value). Notre also that [0123456789] is equal to [0-9].


-


]



.hasMatch(value)


[0123456789]


[0-9]



As for the second pattern, you can remove the digit range from the regex (as you need to allow it) and add a s pattern (s matches any whitespace char) to disallow whitespace in the input:


s


s


RegExp(r'[!@#<>?":_`~;[]\|=+)(*&^%s-]').hasMatch(value)



Create a static final field for the RegExp to avoid creating a new instance every time a value is checked. Creating a RegExp is expensive.


static final RegExp nameRegExp = RegExp('[a-zA-Z]');
// or RegExp(r'pL'); // see https://stackoverflow.com/questions/3617797/regex-to-match-only-letters
static final RegExp numberRegExp = RegExp(r'd');



Then use it like


validator: (value) => value.isEmpty
? 'Enter Your Name'
: (nameRegExp.hasMatch(value)
? null
: 'Enter a Valid Name');





Both are not working. In the question I specified I need something that will not accept anything else other than alphabetical letters, for the name. And for the number input, numbers only not a special character or anything else. This regex thing is awfully stressful
– Kevin Robert
Aug 24 at 17:32





eg. for the name if it is gunter zochbauer if a comma is present somewhere it becomes wrong
– Kevin Robert
Aug 24 at 17:35





You demanded "only letters". Did you check the commented out alternative. If this us not what you want please formulate your question more precise.
– Günter Zöchbauer
Aug 24 at 17:50





i have edited the original question to further explain it. Apologies
– Kevin Robert
Aug 24 at 18:08





I still don't think it is unclear how my answer does not answer your question. RegExp('[a-zA-Z ]') validates to true for my name.
– Günter Zöchbauer
Aug 24 at 20:12


RegExp('[a-zA-Z ]')


true






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ

Node.js puppeteer - Use values from array in a loop to cycle through pages