promp doesn't work

L

little susane

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

Evertjan.

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;'>



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

</table>

You did not declare a table.
<script type="text/javascript" language="Javascript">

<!--

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

little susane

Thx



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

<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.';
  };

};

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top