+ 2
I need help with Javascript Validation!
Hello guys, I have created a custom vendorPortal which requires to valid certain type of vendor names only, but i am unable to valid them through javascript. Requirements: A vendor name can only contain Alphabets- Capital case, Small case and Specific Symbols: ().@ If the input is valid then submit the form else show the alert! Source Code: https://code.sololearn.com/WBUjuCiMT6tO
3 Answers
+ 3
It is always useful to separate the "business logic" of your program in self contained functions, so that it is more clear what happens in your code.
At least the validation logic, and the event handling should be separated. (maybe you would want to use the same validation in multiple parts of your code or website).
Also it improves the structure of your code, if you don't mix the CSS and JS with the HTML code.
An example to check if a string is valid for your criteria, and some tests:
const isVendorValid = (name) =>
/^[A-Za-z().@]+$/.test(name);
console.log(isVendorValid('')); // false
console.log(isVendorValid('Hola!')); // false
console.log(isVendorValid('My.Name(real)')); //true
this is the power of regular expressions :)
+ 4
There is a regex tutorial in Sololearn too, in community content.
https://www.sololearn.com/learn/9704/?ref=app
Besides there are numerous tutorials and thick books about it :) I can't recommend any specific one but they are easy to find with Google.
What I can recommend is this site to test, tweak and understand your regex:
https://regexr.com/
best viewed on a desktop computer but it is an excellent resource.
+ 2
Tibor Santa
Thank you so much for the reply. Yes, I have kept the business logic files in separated .js . It was a simple page program so I embedded the script into the html to simplify the question. I don't know why it wasn't working with the ASCII char codes :/
Any resources would you recommend to learn regular expressions? I have always find it hard to generate the patterns.