Simple Regex for Pattern Validation

Hello,

I am trying to figure out how to create a simple regex for a pattern validation. I want the user to input a specific word (“apples”, for example), and for it to accept only the word “apples” being case-insensitive (i.e. “apples”, “Apples”, “APPLES” would all work). I’ve tried using the site https://regex101.com/ to help me in creating a regex; however, it isn’t working for me. I’m not sure if Wix has its own syntax that isn’t the same as javascript. As you can see in the image I’ve attached, I tried “/apples/i” which should accept the word “apples” as case-insensitive but when I test it while previewing the website, it doesn’t work. I feel like I’m missing something really obvious. Any help would be greatly appreciated.

Best,
Indigo

stcroppe indicated that he “had some problems with the regexp validation on the text input settings. For some reason the wix validation wasn’t seeing the data in the input element.”

Extrapolating from Steve’s input, you might try the following:

$w(’ #< name of your input control>‘).onCustomValidation((value, reject) => {
console.log(JSON.stringify($w(’ # < name of your input control>‘).validity));
let regExp = new RegExp(’/apples/i’);
if (regExp.test(value))
{
console.log(‘You entered “apples” or one of its variants’);
}
else
{
reject(‘You did not enter “apples” or one of its variants’);
}
});

I tried entering this into the code panel for that page (filling in the name of my input control), but now it accepts literally any input that I type in the field, not just variants of “apples”. Any clue what’s going on? Thanks for you help in this!

Well, if regular expressions aren’t going to play nice, how 'bout something like the following:

$w('#<name of your input control>').onCustomValidation((value, reject) => {
  console.log(JSON.stringify($w('#<name of your input control>').validity));
  if (value.toLowerCase() === 'apples')
  {
    console.log('You entered "apples" or one of its variants');
  }
  else
  {
    reject('You did not enter "apples" or one of its variants');
  }
});

?

(I call the above technique “normalizing the case”.)

Yesss!! It works now. Thank you so much! This helped me tremendously. I really appreciate it.

You’re welcome (someone else might be able to figure out a technique using a regular expression but it’s not nice to have alternatives).

Sorry to bother you again. I’m using the same code as before, but now when I publish the site and try typing in “apples”, it doesn’t accept it. It also doesn’t work on any other computer or mobile device. However, it still works perfectly when I’m in the preview mode for the site using the laptop that I’m building the site on. Any ideas on why the code doesn’t seem to be executing correctly once it’s published or while it’s opened on other devices? Thank you!

Is anything getting written out to the Console?

What do you mean by written out to the Console? When I open the page in Preview mode and I open the code panel at the bottom, I can see the code execute correctly when I type into the input box. Is there something more I need to do in “releasing” the block of code you wrote for me, so it works on the published site?

I am trying to create 3 regexes
1st regex:- To validate that an entry is indeed an e-mail address
2nd regex:- To ensure one particular e-mail address is not used more than once to register for another account
3rd regex: To ensure one particular Telephone number is not used more than once to register for another account

hey Aber, could you help me? im trying to adapt this in a way it can set a phone number to pattern, but code does not work, dont know exactly why. it rejects any and ever kind of number pattern now

note: the pattern of number i need the input to aprove is: (15)123456789
How can i do it properly? I tryed to replace the:

if (value.toLowerCase() === 'apples')]

for

if (value === '^\([0-9]{2}\)[0-9]{9}$')

but i guess it must be wrong :stuck_out_tongue:

Is there a way to have some sort of validation to check if a URL contains a certain web address. So for example, if they were to type a URL with the word google.com in it, it would throw up an error?

This is a very old post you are commenting on. Please repost your question in a new post so more people will see it. Clearly explain what you are trying to do and what you want to accomplish.

This post is being closed.