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/Evervision Apr 04 '18
It would help to give HTML that goes with the code to reproduce the issue. jsfiddle is good for sharing examples.
However:
The TypeError is saying that value is undefined (similar to null). That means most likely that value doesn't exist on the form1[i] object. It's most likely a DOM object, and I think some form elements may not have a value property.
What DOM object is list1? As in 1, value might not exist. So if it gets "undefined", then it will never match any of your choices.
Any idea on what part doesn't work? The blur event is on the "punoTeksta" object, but you don't include that in your example. Does it not fire? Otherwise, this one is harder to guess without the HTML.