Travel news - Kamala - Submit articles - Wordpress Themes - Debt Consolidation

PDA

View Full Version : Javascript problem....


Majo Rawne
Jun 21st 2007, 1:21 am
Im trying to write a small Javascrpit program and this is what it consists of....(I havent been doing Javascript for long and I need help)

I need the browser to prompt me to enter a 8 or more letter word and keep prompting till I enter one...

I then need it to prompt me for a number but the number can be no higher than the number of characters in the word entered in the first prompt....

and when i put in this number it will display a number ( the number is explained below)

Word entered: Tottenham
Number Entered: 5
Number displayed on the screen will be: 1

beacuse the number 5 in tottenham is the letter 'e' it then counts how many 'e's are in the word and displays it

Im really stuck

Please help

Majo Rawne:confused:

DavidMerrilees
Jun 21st 2007, 4:23 am
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" >
<head>
<title>form</title>
<style type="text/css">
.none {
display: none;
}

.warn {
color: red;
}
</style>
<script type="text/javascript">
function initialiseForm() {
var legendObj = document.getElementById("legend");
var wordInput = document.getElementById("word");
var numberInput = document.getElementById("number");
var countResultSpan = document.getElementById("countResult");
var wordResultSpan = document.getElementById("wordResult");
var stage1Div = document.getElementById("stage1");
var stage2Div = document.getElementById("stage2");
var wordLbl = stage1Div.getElementsByTagName("label")[0];
var numberLbl = stage2Div.getElementsByTagName("label")[0];
wordInput.onkeyup = function() {
if (this.value.length > 8) {
this.reset();
stage2Div.style.display = "block";
numberInput.reset();
legendObj.innerHTML = "Enter a number";
numberInput.test(true);
} else {
stage2Div.style.display = "none";
if (this.value.length > 0) {
wordLbl.className = legendObj.className += " warn";
legendObj.innerHTML = "Word must be longer than 8 characters";
} else {
this.reset();
}
}
}
wordInput.reset = function() {
wordLbl.className = "";
legendObj.reset();
}
numberInput.onkeyup = function() {
this.test(false);
}
numberInput.reset = function() {
numberLbl.className = "";
legendObj.reset();
}
numberInput.test = function(soft) {
if (!isNaN(parseInt(this.value - 1)) && this.value.length > 0) {
if ((this.value - 1) <= wordInput.value.length && (this.value - 1) > 0) {
var letter = wordInput.value.charAt(this.value - 1);
var instances = 0;
for (var i = 0; i <= wordInput.value.length; i++) {
if (letter == wordInput.value.charAt(i)) instances++;
}
numberInput.reset();
legendObj.innerHTML = "There are <strong>" + instances + " " + letter + "</strong> 's in <strong>" + wordInput.value + "</strong>";
} else {
numberLbl.className = legendObj.className += " warn";
legendObj.innerHTML = "Number must be less than the number of characters in your word";
}
} else if (!soft) {
numberLbl.className = legendObj.className += " warn";
legendObj.innerHTML = "Enter a valid number";
}
}
legendObj.reset = function() {
this.className = "";
this.innerHTML = "Enter a word";
}
wordInput.value = "";
numberInput.value = "";
}

window.onload = function() {
initialiseForm();
}
</script>
</head>
<body>
<fieldset>
<legend id="legend">Enter a word</legend>
<div id="stage1">
<label for="word">Word</label>
<input id="word" type="text" />
</div>
<div class="none" id="stage2">
<label for="number">Number</label>
<input id="number" type="text" />
</div>
</fieldset>
</body>
</html>

Majo Rawne
Jun 21st 2007, 4:59 am
Wow thanks for the quick reply..

But i need it to be alot simplier than that mate

Ive just started Javascript learning prompts etc... i jst need it to be simply and basic

For eg this is how far i got

<HTML>
<HEAD>

<TITLE>Enter a Word </TITLE>

<SCRIPT language="JavaScript" >

var mysring; // string entered by user
var userInput

//initialise myString to an empty string
mystring ='';

userInput = window.prompt('Please Enter A Word','');
userInput = parseFloat (userInput);

function checkLength(userInput)
{
return (userInput.length >=8)

}
//loop while valid input has not been recieved


</SCRIPT>

</HEAD>

<BODY>

</BODY>

</HTML>

Could you do it like i have above, I know what above doesnt work but as i said im new to Javascript

Majo Rawne
Jun 21st 2007, 5:00 am
By looking at your code i want to be as good as you in the future:D