I am trying to use regulare expressions with a formvalidator.net to validate a form but I am struggling to under stand regular expressions,I have had a look at some websites but it is still baffling.
Basically I want to validate a field so that when a user enter something it will chack that the word or entry doesnt contain certain characters. So for example I don't want '!name' or 'this ! name' to be accepted.
using regexp for testing I have created this expression ([a-zA-Z\+\&\,\.\-\(\)\d][^<>%$!]+) which excludes those characters but in my form validator it will still accept '!name' or 'this ! name'
Can anyone point me in the right direction? I feel like I'm typing stuff in and gettign nowhere.
Ian
Basically I think you want to either decide what characters you don't want OR what characters you do. Not both.
I think this is what your after:
^[^<>%$!]+$
See it working here
Related
I have a Flutter TextFormField for email with input formatter as below.
var emailAddressFormatter = FilteringTextInputFormatter.allow(RegExp(
r"[a-zA-Z0-9.a-zA-Z0-9.!#$%&'*+-/=?^_`{|}~]+#[a-zA-Z0-9]+\.[a-zA-Z]+"));
The problem is, when trying to input any character in the field it does not allow. The regex looks fine to me. When the formatter is removed the field accepts any character with any format. What am I going wrong?
The issue is that the FilteringTextInputFormatter that you're using rejects anything that does not match your regex. When you enter just a single character, it does not match your regex, so the character is rejected.
I know little about regex so I'm not sure if it's possible, but you would need a regex that would be able to match every string as you type e.g. a, am, amani#, amani#gmail.com.
I would personally not try to do filtering such as this. Instead, I would just allow all valid characters that are valid in email addresses to be present in the email and not enforce the specific format with the # and .. Then I would use a validator to check that the email is valid upon form submission.
If you don't like the alternate solution I proposed above and you can't use regex, you can make your own input formatter quite easily with TextInputFormatter.withFunction.
Lets say I have something like this:
vin#text.com
jay#text.com
text#text.com
All these id's belong to #text.com, and I want to match just that, whether an id has a
#text.com
or not
for example:
vin#gmail.com is invalid
while
vin#text.com is valid
but
vin#text.com.com , vin#text.com#text.com are both invalid
there should not be any characters after #text.com but there can be as many as possible in the beginning
So, text.com can be treated as required after # for your scenario.
Simple regex could be
^[a-zA-Z0-9+_.-]+#text.com+$
Please have a look if this works for you.
Happy Coding!
I currently have a contact form where I reject values in text fields that have email addresses and URLs. I use these REGEX expressions in my Rails controller. I want to replicate this and use it in my HTML pattern field instead.
regex_email = /^([a-zA-Z0-9_\-\.]+)#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/
regex_url = /^(http|ftp|https)|[.][a-zA-Z][a-zA-Z]/
regex_message = /seo|captcha|sales|Б|Г|Д|ё|Ж|П|Ф|И|й|Л|Ц|Ш|Щ|Э|Ю|Я/i
regex_all = /#{regex_email}|#{regex_url}|#{regex_message}/i
I have created locale hashes for regex_url, regex_message, and regex_url. I want to negate hose values then add AND operators of some kind and use something like my example below as my pattern.
"data-pattern" => "NOT#{regex_email}AND NOT#{regex_url}AND NOT#{regex_message}"
Either this or something like this where the negation of my hashes is added to the hash itself.
"data-pattern" => "#{regex_email}AND#{regex_url}AND#{regex_message}"
I've searched a good number of links about AND and NOT but the discussions are not really clear.
I'm asking this question because I have not been successful in coding regex expressions that only include letters, numbers, period, or hyphen. All the solutions I found still allowed any single character when I included the . in my regex. I even tried escaping it but it still matches.
Assuming that your expressions regex_email, regex_url and regex_message are working reliably, and that you're looking for a logical negation of your regex_all pattern on the regex level (that is how I understand your question).
You can make use of a slightly modified negative lookahead logic, like so:
/^(?!.*#{regex_email})(?!.*#{regex_url})(?!.*#{regex_message})/
I'm using an application called Firemon which uses regex to pull text out of various fields. I'm unsure what specific version of regex it uses, I can't find a reference to this in the documentation.
My raw text will always be in the following format:
CM: 12345
APP: App Name
BZU: Dept Name
REQ: First Last
JST: Text text text text.
CM will always be an integer, JST will be sentence that may span multiple lines, and the other fields will be strings that consist of 1-2 words - and there's always a return after each section.
The application, Firemon, has me create a regex entry for each field. Something simple that looks for each prefix and then a return should work, because I return after each value. I've tried several variations, such as "BZU:\s*(.*)", but can't seem to find something that works.
EDIT: To be clear I'm trying to get the value after each prefix. Firemon has a section for each field. "APP" for example is a field. I need a regex example to find "APP:" and return the text after it. So something as simple as regex that identifies "APP:", and grabs everything after the : and before the return would probably work.
You can use (?=\w+ )(.*)
Positive lookahead will remove prefix and space character from match groups and you will in each match get text after space.
I am a little late to the game, but maybe this is still an issue.
In the more recent versions of FireMon, sample regexes are provided. For instance:
jst:\s*([^;]?)\s;
will match on:
jst:anything in here;
and result in
anything in here
How can I use regular expression attribute in MVC3 on EMAIL field to give an error message if the email entered contains no-email.com?
The exact syntax will depend on the language you are using and possibly the method you are using. These examples should help.
You wouldn't normally need a regular expression to match a simple string.
But, if for some reason, it has to be regex, you would just need to escape the hyphen and dot. Like so:
no\-email\.com
Depending on what you are doing, you may need to match the rest of the email address:
(.*?)no\-email\.com
You may also want to tie "no-email.com" to the end of the string, like so:
(.*?)no\-email\.com$
If you also want to match the # sign to the domain name, do:
(.*?)#no\-email\.com$