By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,712 Members | 2,116 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,712 IT Pros & Developers. It's quick & easy.

promp doesn't work

P: n/a
Hi,
I am trying to change "print results" for promt.
I don't want the output to be in HTML, it should be alert boxes.It
should be fairly easy, but every time i am changing the - >
results.innerHTML = out; it stops working.

thanks
<head>
<body>

<input id='phrase' type='text'><br>
<input type='submit' value='Click to check for palindrome'
onClick='validate();return false;'>
<span id='results'></span>
</td>
</tr>

</table>

<script type="text/javascript" language="Javascript">
<!--

var forward;
var backward;

function validate()
{
var s = "";
forward = "";
backward = "";

var p = document.getElementById( "phrase" ).value;

if ( p == null || p.length == 0 ) {
printResults(0, "A blank string is not valid. Try entering
some text.");
}

p = p.toLowerCase();

var regex = new RegExp( /[a-z]/ );

for (i = 0; i < p.length; i++) {
if ( regex.test( p.charAt( i ) ) ) {
s += p.charAt( i );
}
}

for (i = 0; i < s.length; i++) {
forward += s.charAt( i );
backward += s.charAt( s.length - 1 - i );
}

if ( forward == backward )
{
printResults( 1 );
}
else
{
printResults( 0 );
}
}

// 1 for valid, 0 for not valid

function printResults( isValid, msg )
{
var valid = "valid palindrome.";
var not_valid = "NOT a valid palindrome.";

var results = document.getElementById( "results" );
var p = document.getElementById( "phrase" ).value;

var out = "Results";

if ( isValid ) {
out += "Yup, " + p + " is a " + valid;
}
else {
out += "Nope, " + p + " is " + not_valid;
}

out += "<br>";

if ( msg != null && msg.length 0 ) {
out += msg + "<br>";
}

if ( !isValid && forward.length 0 && backward.length 0 )
{
out += "[" + forward + "] (forward)<br>" +
"[" + backward + "] (backward)";
}

results.innerHTML = out;
}

function clearFields()
{
document.getElementById( "results" ).innerHTML = "";
var p = document.getElementById( "phrase" );

p.value = "";
p.focus();
}

clearFields();

//-->
</script>
</body>
</html>
Aug 8 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
little susane wrote on 08 aug 2008 in comp.lang.javascript:
Hi,
I am trying to change "print results" for promt.
I don't want the output to be in HTML, it should be alert boxes.It
should be fairly easy, but every time i am changing the - >
results.innerHTML = out; it stops working.

thanks
<head>
<body>

<input id='phrase' type='text'><br>
<input type='submit' value='Click to check for palindrome'
onClick='validate();return false;'>
As You did not declare a <form>,
the return false is not necessary.
>

<span id='results'></span>
</td>
</tr>

</table>
You did not declare a table.
<script type="text/javascript" language="Javascript">
<script type='text/javascript'>
will do.
<!--
do not use <!-- in the 21st century.
var forward;
var backward;

function validate()
{
var s = "";
forward = "";
backward = "";

var p = document.getElementById( "phrase" ).value;

if ( p == null || p.length == 0 ) {
a text input field can be zero length not null
printResults(0, "A blank string is not valid. Try entering
some text.");
}

p = p.toLowerCase();

var regex = new RegExp( /[a-z]/ );

for (i = 0; i < p.length; i++) {
if ( regex.test( p.charAt( i ) ) ) {
s += p.charAt( i );
}
}

for (i = 0; i < s.length; i++) {
forward += s.charAt( i );
backward += s.charAt( s.length - 1 - i );
}

if ( forward == backward )
{
printResults( 1 );
}
else
{
printResults( 0 );
}
}
Wow, the whole ting can be done like this:

================= palindrome.html =========================
<input id='phrase' type='text'><br>
<input type='submit'
value='Click to check for palindrome'
onClick='isPalindrome();'>
<div id='results'></div>
<script type='text/javascript'>

function isPalindrome(t){
var p = document.getElementById("phrase").value;
var r = document.getElementById( "results" );
var forward = p.toLowerCase().replace(/[^a-z]/g,'');
if (forward.length>1) {
var backward = forward.split('').reverse().join('');
var pal = (forward == backward ) ?'' :'NOT ';
r.innerHTML = '"' + p + '" is ' + pal + 'a valid palindrome.';
} else {
r.innerHTML = 'String needs at least two letters.';
};
};

</script>
==========================================

// 1 for valid, 0 for not valid
Why? Use true and false in js.
>
function printResults( isValid, msg )
you did not declare msg

{
var valid = "valid palindrome.";
var not_valid = "NOT a valid palindrome.";

var results = document.getElementById( "results" );
var p = document.getElementById( "phrase" ).value;
again?
var out = "Results";

if ( isValid ) {
out += "Yup, " + p + " is a " + valid;
}
else {
out += "Nope, " + p + " is " + not_valid;
}

out += "<br>";

if ( msg != null && msg.length 0 ) {
out += msg + "<br>";
}

if ( !isValid && forward.length 0 && backward.length 0 )
{
out += "[" + forward + "] (forward)<br>" +
"[" + backward + "] (backward)";
}

results.innerHTML = out;
}

function clearFields()
{
document.getElementById( "results" ).innerHTML = "";
var p = document.getElementById( "phrase" );

p.value = "";
p.focus();
}

clearFields();
You clear at the loading of the page, the fileds are emptty anyway.

//-->
do not use, see above.
</script>
</body>
</html>


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Aug 8 '08 #2

P: n/a
Thx
<HTML>

<head>
<body>
* * * * <input id='phrase' type='text'><br>
* * * * <input type='submit' value='Click to check for palindrome'
onClick='validate();return false;'>
As You did not declare a <form>,
the return false is not necessary.
* * <span id='results'></span>
* * </td>
</tr>
</table>


You did not declare a table.
what for do I need to declare a table?

<script type="text/javascript" language="Javascript">

<script type='text/javascript'>
will do.

<!-- do not use <!-- in the 21st century.
<!--ZOOM&STOP--><!---what do we use then for html comments?---->

var forward;
var backward;
function validate()
{
* * var s = "";
* * forward = "";
* * backward = "";
* * var p = document.getElementById( "phrase" ).value;
* * if ( p == null || p.length == 0 ) {

a text input field can be zero length not null
* * * * printResults(0, "A blank string is not valid. *Try entering
some text.");
* * }
* * p = p.toLowerCase();
* * var regex = new RegExp( /[a-z]/ );
* * for (i = 0; i < p.length; i++) {
* * * * if ( regex.test( p.charAt( i ) ) ) {
* * * * * * s += p.charAt( i );
* * * * }
* * }
* * for (i = 0; i < s.length; i++) {
* * * * forward += s.charAt( i );
* * * * backward += s.charAt( s.length - 1 - i );
* * }
* * if ( forward == backward )
* * {
* * * * printResults( 1 );
* * }
* * else
* * {
* * * * printResults( 0 );
* * }
}

Wow, the whole ting can be done like this:
I am sure there are different ways to do the same thing.
You can write it in C++ and python, i guess..

From what I see ..the results of validation in your code are still
going to show in html
Not as a prompted answer.
Thanks for your comments, i did improve code a bit.

>
================= palindrome.html =========================
<input id='phrase' type='text'><br>
<input type='submit'
value='Click to check for palindrome'
onClick='isPalindrome();'>
<div id='results'></div>

<script type='text/javascript'>

function isPalindrome(t){
* var p = document.getElementById("phrase").value;
* var r = document.getElementById( "results" );
* var forward = p.toLowerCase().replace(/[^a-z]/g,'');
* if (forward.length>1) {
* * *var backward = forward.split('').reverse().join('');
* * *var pal = (forward == backward ) ?'' :'NOT ';
* * *r.innerHTML = '"' + p + '" is ' + pal + 'a valid palindrome.';
* } else {
* * *r.innerHTML = 'String needs at least two letters.';
* };

};

</script>
==========================================
// 1 for valid, 0 for not valid

Why? Use true and false in js.
function printResults( isValid, msg )

you did not declare msg
{
* * var valid = "valid palindrome.";
* * var not_valid = "NOT a valid palindrome.";
* * var results = document.getElementById( "results" );
* * var p = document.getElementById( "phrase" ).value;

again?
* * var out = "Results";
* * if ( isValid ) {
* * * * out += "Yup, " + p + " is a " + valid;
* * }
* * else {
* * * * out += "Nope, " + p + " is " + not_valid;
* * }
* * out += "<br>";
* * if ( msg != null && msg.length 0 ) {
* * * * out += msg + "<br>";
* * }
* * if ( !isValid && forward.length 0 && backward.length 0 )
* * {
* * * * out += "[" + forward + "] (forward)<br>" +
* * * * * * "[" + backward + "] (backward)";
* * }
* * results.innerHTML = out;
}
You clear at the loading of the page, the fileds are emptty anyway.
do not use, see above.
</script>
</body>
</html>

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Aug 8 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.