Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / NewStats: 3,194,431 members, 7,954,695 topics. Date: Saturday, 21 September 2024 at 07:27 AM |
Nairaland Forum / Science/Technology / Programming / Could This Basic Code Be Malfunction? (3175 Views)
Help Me Cross Check My Visual Basic Code / Need Help On This Basic Php / Need Help With This Basic Php (2) (3) (4)
Could This Basic Code Be Malfunction? by paranorman(m): 6:11pm On Jul 06, 2015 |
<script type="text/javascript"> //<![CDATA[ function validate() { var regex= document.getElementById("regex".value; var text= document.getElementById("text".value; var regexRE= /regex/; if (text.match(regexRE)) { alert("Hallelujah, it matches!" } else { alert("it doesn't match, check your code son!" } //end if } //end function //]]> </script> </head> <body> <form action=""> <fieldset> <label>regex</label> <input type="text"id="regex"/> <label>Working text</label> <input type="text"id="text"/> <button type="button" onclick="validate()">Validate</button> </fieldset> </form> this code is suppose to validate text input against the regex entered in the regex field. but it keeps alerting "it doesnt't match... ..." even when i intentionally enter a match. what could b wrong? Is it my PC or the code? I even tried using different editors. |
Re: Could This Basic Code Be Malfunction? by paranorman(m): 6:15pm On Jul 06, 2015 |
All brackets are closed.. Ignore those that appears unclosed. NL did that.. 1 Like |
Re: Could This Basic Code Be Malfunction? by Nobody: 6:58pm On Jul 06, 2015 |
If you type in "regex" as the working text, it will match. . . . . . .this is because you have a logical error on line 8 . . .which i have altered below to eval('var regexRE= /'+regex+'/g;'); plus i also removed the nairaland smiles So in essence, the proper working code should be: <script type="text/javascript"> |
Re: Could This Basic Code Be Malfunction? by paranorman(m): 8:06pm On Jul 06, 2015 |
dhtml18: thanks, I got it now.. |
Re: Could This Basic Code Be Malfunction? by Nobody: 9:37pm On Jul 06, 2015 |
You are most welcome. . . . |
Re: Could This Basic Code Be Malfunction? by GuiltySpark343(m): 10:19pm On Jul 06, 2015 |
dhtml18: badass *Am adding this one to my followed list |
Re: Could This Basic Code Be Malfunction? by Nobody: 6:02am On Jul 07, 2015 |
Hahahaha. . . .that is coding whizkid you got on board |
Re: Could This Basic Code Be Malfunction? by blueyedgeek(m): 6:49am On Jul 07, 2015 |
dhtml18:I've read books that recommend that eval be avoided at all costs since it messes with the scope and is also a security hazard since it can be used to evaluate expressions as strings. Why did you decide to use eval? |
Re: Could This Basic Code Be Malfunction? by blueyedgeek(m): 6:52am On Jul 07, 2015 |
@op, adding event listeners as html attributes is considered bad practice. Ever heard about separation of concerns? |
Re: Could This Basic Code Be Malfunction? by Nobody: 7:13am On Jul 07, 2015 |
blueyedgeek:Yes, your book was of course right. Even if you are going to evaluate anything, it should not be user's input - due to security reasons. However, the challenge the op has presented is best solved with EVAL. I have not used EVAL in any real-life project in the actual fact. But as for the scope thing, you can always fix that by targeting the correct scope when evaluating. |
Re: Could This Basic Code Be Malfunction? by paranorman(m): 7:35am On Jul 07, 2015 |
blueyedgeek:i ain't no javascript geek, nah. I wanna mess with sql/MS Access and I'm using the browser to test RegEXs.. Care to explain to me better? |
Re: Could This Basic Code Be Malfunction? by paranorman(m): 8:08am On Jul 07, 2015 |
dhtml18:i am even thinking of javascript full time now self. Which comprehensive book do you recommend? |
Re: Could This Basic Code Be Malfunction? by Nobody: 8:20am On Jul 07, 2015 |
^^^I think you might have to ask blueyedgeek, I have been using JavaScript for about 15yrs now on rough estimate. I haven't gotten any textbooks. . . . . |
Re: Could This Basic Code Be Malfunction? by Nobody: 8:41am On Jul 07, 2015 |
paranorman: speaking Javascript |
Re: Could This Basic Code Be Malfunction? by paranorman(m): 8:42am On Jul 07, 2015 |
blueyedgeek:recommend any comprehensive book on javascript I can read out? |
Re: Could This Basic Code Be Malfunction? by paranorman(m): 8:43am On Jul 07, 2015 |
Jregz:that's just it? I google 'speaking Javascript'? |
Re: Could This Basic Code Be Malfunction? by Nobody: 8:46am On Jul 07, 2015 |
paranorman: Yup.... speakingjs.com/ |
Re: Could This Basic Code Be Malfunction? by Nobody: 8:53am On Jul 07, 2015 |
I am sure my javaScript is outdated, I shall look for a recent book to brush myself up. |
Re: Could This Basic Code Be Malfunction? by Nobody: 8:55am On Jul 07, 2015 |
I just downloaded it from http://kat.cr/usearch/speaking%20javascript/ |
Re: Could This Basic Code Be Malfunction? by paranorman(m): 8:57am On Jul 07, 2015 |
dhtml18:so you'v been doing JS for 15 years? Interesting. Any other language thou knowest? |
Re: Could This Basic Code Be Malfunction? by Nobody: 9:10am On Jul 07, 2015 |
^^^maybe. . . . |
Re: Could This Basic Code Be Malfunction? by Nobody: 10:03am On Jul 07, 2015 |
The Speak JavaScript book is really good and I shall recommend it for anymore. Skimming through it at hyperspeed, I am already on page 65 and I have learnt a good number of lessons already - some things I have encountered before but did not really understand until now. The most important one being working in Strict mode, until now I have only been able to work in sloppy mode - but struggle greatly when in strict mode. While you might think this is not important, but when you are editing a code that is already in strict mode, you will really sweat. After getting to page 160 or so, I have already learnt quite a lot of new things in JavaScript. That now made me understand what they meant at the begining of the book when the author said: Who this book is forSo based on this - I can see that I fall into the category of JavaScript programmers who want to deepen their skills. This book is not for pure beginners who just want to learn JavaScript (without a background knowledge of at least one OOP language e.g. JAVA) - because there are so many things there that are useless to a beginner who just want to get started with JavaScript. |
Re: Could This Basic Code Be Malfunction? by paranorman(m): 10:25am On Jul 07, 2015 |
dhtml18: that's great. |
Re: Could This Basic Code Be Malfunction? by blueyedgeek(m): 6:42am On Jul 09, 2015 |
paranorman:Sorry, I wasn't able to reply earlier. Search for proffesional javascript for web developers. It's for both beginners and experts alike and it's the most understandable and comprehensive book I've come across. |
Re: Could This Basic Code Be Malfunction? by paranorman(m): 7:02am On Jul 09, 2015 |
blueyedgeek:thanks.. You wrote about not using event listeners as attributes. Discuss. |
Re: Could This Basic Code Be Malfunction? by blueyedgeek(m): 9:02pm On Jul 09, 2015 |
paranorman:By tying your event listeners to your html, you've basically locked it to that page that it appears on and it can't be re-used. If you decided to change the name of your function, you would change it both in the javascript and the html. There's a software principle known as separation of concern and it posits that the layers that make up a web page should be kept separate from each other. Keep html as html, css as css and javascript as javascript, heck even your server side stuff should be on it's own as well. You just have to write your code in such a way that it allows all parts to interact without getting in each others way. Use html to link to your css file, also use html to link to your javascript files and use those files to connect with the html using hooks from the html. Things like class names, ids, data-attributes are there for your usage. 2 Likes |
Re: Could This Basic Code Be Malfunction? by Nobody: 2:33am On Jul 10, 2015 |
^^^This dude is turning into the jscript geek: @op, this is an example of what he is talking about: notice that the onclick event is added before the html code. This method is a shortcut and has its own limitations though. . . .but the code works crossbrowser i should think, but it is still a bit old-school. <script type="text/javascript"> |
Re: Could This Basic Code Be Malfunction? by oyebolasdk(m): 6:29am On Jul 10, 2015 |
dhtml18:chairman abeg recommend language and book for me I just wan start programming. Pls, I need it urgently, Thanks in advance |
Re: Could This Basic Code Be Malfunction? by paranorman(m): 7:54am On Jul 10, 2015 |
blueyedgeek:oshey! I get what you mean now.. Thanks. |
Re: Could This Basic Code Be Malfunction? by paranorman(m): 7:59am On Jul 10, 2015 |
dhtml18:oshey! Okay, you gave a shortcut and I don't like learning starting wiith shortcuts, nah! Show me the 'professional' way, I beg thee. Thank you.. |
Re: Could This Basic Code Be Malfunction? by Nobody: 9:19am On Jul 10, 2015 |
Hmn, let us use event listeners then: <script type="text/javascript"> For those looking for book, please follow recommendations of blueyedgeek - he is more current than I. . . . .I am relic of an old age |
Re: Could This Basic Code Be Malfunction? by paranorman(m): 9:27am On Jul 10, 2015 |
dhtml18:i think i have to go deep on JS.. Thanks. |
How Profitable Is Android App Development In Nigeria? / Learn Git And Github [*ARCHIVED*] / Arduino Visual Embedded Programming With Flprog
(Go Up)
Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health religion celebs tv-movies music-radio literature webmasters programming techmarket Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 52 |