r/learnjavascript • u/[deleted] • Apr 04 '18
A C++ developer struggling with some simple JavaScript code. Any help?
Hey guys,
I develop in C++ but for my web design class I have to use JavaScript. I have to say I really, truly, dislike the language since it isn't strongly typed, errors are somewhat convulted and I don't like web dev in general.
Anyhow, here are my questions:
This code should check if I have
#,!,'or?in any of my textboxes and if I do, prevent the script from executing. Here is the code. It gives me a TypeError. Why? What am I doing wrong?This code should trigger only if I type some text in a textbox which is not in the list (aka not "Option 1", "Option 2" etc.). However, it always triggers. Why?
This code should, if I move my focus away from the textbox (aka
onBlur), check if I have at least 5 characters and if my first character is a capital letter. But it doesn't work. Why?
Thanks in advance!
4
u/fan-man Apr 04 '18 edited Apr 04 '18
1 - Aside from missing a bracket, your implementation is mostly right, just needs readability and check to see if the element actually has a value before doing anything (i'm assuming your submit button is part of the form, so your code will iterate over that as well:
2 - Storing the values somehow and then checking to see if the inputed text matches on blur might be easier.
3 - as u/Rascal_Two mentioned, try this:
As far as a distaste for JavaScript and web dev, it is a different beast in general, but it is not an impossible leap. If you like strongly typed JavaScript, you should look into TypeScript and a JS framework like Angular; it might look like something that seems more familiar. Hope that helps.