The built-in validation uses preset rules which will “mark” the element (usually with a red border) if the value is invalid. You can also check the validity status yourself. Take a look at $w.TextInput.valid as an example. The example code included under this API…
let isValid = $w("#myElement").valid;
…can be used to check the validity of the element’s value wherever you need. You could check it in the onChange() function of the element, or in the onClicked() routine of a button on the page.
If one of the built-in validations isn’t suitable for your purposes, you can always set a custom validation right from the settings window of the element:
You can work out your regex expression on your own, or use any number of regex expressions available via a search on the Internet. Take a look at regex101 for lots of information and tools.
Hopefully that will give you a good jump start. Let me know how it goes.
What you have sent is perfect. I had the validation bit correct (and also use regex101) but needed to get something a bit more than the “red box outline”.
Your pointers were exactly what I needed - many thanks!!
The .valid property does not work properly during a blur event with URL field types
Example:
A form has a field set to type URL.
Setup a blur event for the field with the code:
console.log($w(“#URL”).valid);
Test:
Preview the form and click into the field and type “aaa” (or any other string that doesn’t start with HTTP://)
Click out of the field. The console.log shows “true” indicating that the field validation is good. The field will have a red box around it indicating an error.
Click into the field again. Do not change the bad URL in the field.
Click out of the field. The console.log shows “false” indicating that the field validation has failed. The field will have a red box around it indicating an error. (finally a correct response)
Click into the field. Change to a valid URL.
Click out of the field. The console.log shows “false” indicating that the field validation has failed even though the URL is valid. The field will NOT have a red box around it indicating an error.
Clicking in and out of the field one more time yields a true response to the “valid” method.
I have experienced this same behaviour on text input fields trying to use the onBlur event - if you click out of the field the first time after changing it from a non-valid to a valid entry, then .valid will be false, however, it you then simply click back into the field, make no changes at all (so it remains a valid value), and then click back out again, then .valid becomes true. This would definitely seem to be a bug.
Old thread but i’m hoping someone can help me out. On my text input fields I have no options to add additional validations. I would like to add a regular expression pattern validation but there is no option in settings. The only option i have is Required. I don’t have Limit Length or Read Only either
This is specific to a use case for myself, however I wanted to check an email was entered to save spamming after the captcha was completed. So I had custom validation and Regex in $w(“#emailInput”) element, I needed it to be confirmed as a valid email before subsequently hashing the email and then also creating a reference shortly after the confirmation the email was correct. This is how I achieved it.