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