Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,152,936 members, 7,817,728 topics. Date: Saturday, 04 May 2024 at 06:08 PM

Could This Basic Code Be Malfunction? - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Could This Basic Code Be Malfunction? (3113 Views)

Help Me Cross Check My Visual Basic Code / Need Help On This Basic Php / Need Help With This Basic Php (2) (3) (4)

(1) (2) (Reply) (Go Down)

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"wink.value;
var text= document.getElementById("text"wink.value;

var regexRE= /regex/;

if (text.match(regexRE)) {
alert("Hallelujah, it matches!"wink;
} else {
alert("it doesn't match, check your code son!"wink;
} //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">
//<![CDATA[

function validate() {
var regex= document.getElementById('regex').value;
var text= document.getElementById('text').value;

eval('var regexRE= /'+regex+'/g;');

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>
Re: Could This Basic Code Be Malfunction? by paranorman(m): 8:06pm On Jul 06, 2015
dhtml18:
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:


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:
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:


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:
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:

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:
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?
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:
@op, adding event listeners as html attributes is considered bad practice. Ever heard about separation of concerns?
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:
You are most welcome. . . .
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:

i am even thinking of javascript full time now self.
Which comprehensive book do you recommend?

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:


speaking Javascript
that's just it? I google 'speaking Javascript'?
Re: Could This Basic Code Be Malfunction? by Nobody: 8:46am On Jul 07, 2015
paranorman:
that's just it? I google 'speaking Javascript'?

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:
I am sure my javaScript is outdated, I shall look for a recent book to brush myself up.
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 for
This book has been written for programmers, by a programmer. So, in order to
understand it, you should already know object-oriented programming, for example,
via a mainstream programming language such as Java, PHP, C++, Python, Ruby,
Objective-C, C#, or Perl.
Thus, the book’s target audience is programmers who want to learn JavaScript
quickly and properly, and JavaScript programmers who want to deepen their skills
and/or look up specific topics.
So 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:
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:

So 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.

that's great.
Re: Could This Basic Code Be Malfunction? by blueyedgeek(m): 6:42am On Jul 09, 2015
paranorman:

recommend any comprehensive book on javascript I can read out?
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:
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.
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:

thanks.. You wrote about not using event listeners as attributes. Discuss.
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">
//<![CDATA[

function validate() {
var regex= document.getElementById('regex').value;
var text= document.getElementById('text').value;

eval('var regexRE= /'+regex+'/g;');

if (text.match(regexRE)) {
alert('Hallelujah, it matches!');
} else {
alert('it doesn\'t match, check your code son!');
} //end if
} //end function

window.onload=function() {

document.getElementById('btn').onclick=validate;

}
//]]>
</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" id="btn">Validate</button>

</fieldset>
</form>
Re: Could This Basic Code Be Malfunction? by oyebolasdk(m): 6:29am On Jul 10, 2015
dhtml18:
You are most welcome. . . .
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:
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.
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:
^^^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.

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">
//<![CDATA[

function validate() {
var regex= document.getElementById('regex').value;
var text= document.getElementById('text').value;

eval('var regexRE= /'+regex+'/g;');

if (text.match(regexRE)) {
alert('Hallelujah, it matches!');
} else {
alert('it doesn\'t match, check your code son!');
} //end if
} //end function

window.onload=function() {
var elem=document.getElementById('btn');

if (elem.addEventListener) {
// W3C DOM
elem.addEventListener("click",validate,false);
} else if (elem.attachEvent) {
// IE DOM
elem.attachEvent("onclick", validate);
}

}
//]]>
</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" id="btn">Validate</button>

</fieldset>
</form>

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:
Hmn, let us use event listeners then:



For those looking for book, please follow recommendations of blueyedgeek - he is more current than I. . . . .I am relic of an old age
i think i have to go deep on JS..
Thanks.

(1) (2) (Reply)

Asp.net/mssql Versus Php/mysql / Meet Crux, My CRUD ASP.NET MVC 5 Movie Rental Web Application. / 17 Year Old aspiring Frontend Developer / Product Designer

(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. 63
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.