0

Checking against a pattern

How to check an input field against a certain pattern For example: To check an email input against (example@example.com)

22nd Aug 2019, 12:31 PM
Majd Alkwaja
Majd Alkwaja - avatar
8 ответов
+ 4
Well, you can do so with a regular expression: if (/^[\w\d]+@\w+\.com$/.test(inputString)) console.log("Good enough"); else console.log("Not correct"); I did not just slam my head on my keyboard, this regular expression will try to match this: ^: Start of the string [\w\d]+: One or more word character(s)/digit(s) @: @ character \w+: One or more word character(s) \.: . character com: These three charactere exactly $: End of the string
22nd Aug 2019, 12:39 PM
Airree
Airree - avatar
+ 1
I'm a fan of www.regex101.com Here is one I setup to work with a variety of email formats: https://regex101.com/r/pt5qJh/4 Using this RegEx: ^([\w-.]+)@([\w-]+)(\.[\w.-]+)$ Hope this helps.
22nd Aug 2019, 4:55 PM
David Carroll
David Carroll - avatar
0
thanks i think i am going to see videos on regular expressions
22nd Aug 2019, 1:01 PM
Majd Alkwaja
Majd Alkwaja - avatar
0
i wanna ask you if i wanna put it as a part of a comparing operation like if(#id == ) should i put the rgex inside queotes or what
22nd Aug 2019, 2:23 PM
Majd Alkwaja
Majd Alkwaja - avatar
0
No, you cannot compare to regular expressions like that. If you looked at it; regular expressions are enclosed in slashes, so this is a valid regular expression: /^/. There are a few functions you can use to use regular expressions, for example: RegExp.prototype.test RegExp.prototype.exec String.prototype.match String.prototype.replace (and so on) but test is the most logical, as it returns a simple boolean
22nd Aug 2019, 2:35 PM
Airree
Airree - avatar
0
ohh thanks another question if u do not mind🌚 why in ur example /^[\w\d]/ why did put \d even though that \w means letters and digits based on what i watched
22nd Aug 2019, 2:38 PM
Majd Alkwaja
Majd Alkwaja - avatar
0
Because I am stupid
22nd Aug 2019, 2:40 PM
Airree
Airree - avatar
0
🙂 all of this and stupid u have to forgive yourself sometimes i think 🙃
22nd Aug 2019, 2:41 PM
Majd Alkwaja
Majd Alkwaja - avatar