/    Sign up×
Community /Pin to ProfileBookmark

problem with script

(I apologize for the vague title.)
Hello, I’ve got this code that I don’t see any problems with but it only works in certain conditions.

[code=php]
var digit=new Array();
digit[0]=”u3007″;
digit[1]=”u4E00″;
digit[2]=”u4E8C”;
digit[3]=”u4E09″;
digit[4]=”u56DB”;
digit[5]=”u4E94″;
digit[6]=”u516D”;
digit[7]=”u4E03″;
digit[8]=”u516B”;
digit[9]=”u4E5D”;
digit[10]=”u5341″;
digit[100]=”u767E”;
function number_conv(form) {
if (form.roomaji.value >= 0) {
if (form.roomaji.value.charAt(form.roomaji.value.length – 1)==”.”) {
form.textarea.value+=digit[eval(form.roomaji.value)];
form.roomaji.value=””;
}
}
}[/code]

EDIT-for some reason, the forum is splitting up the word ‘length’ above; thats not reason the code isn’t working ๐Ÿ˜ฎ

form.roomaji is just a textbox that numbers will be entered into. When a number is entered, it will check to see if there is a period at the end (like 1.), if so, it will enter the corresponding value (without the period) from the digit array. It works for all one digit numbers (0-9), however it won’t work with 10 or 100 (anything with more than two digits.) Can anyone see a problem?
This is the form that corresponds with this function:

[code=php]
<form name=’form’>
<input name=”roomaji” type=”text” onkeyup=”number_conv(this.form)” size=”25″>
<br>
<textarea name=”textarea” cols=”66″ rows=”25″></textarea>
</form>

[/code]

Thank you in advance for any help.

-Steve

to post a comment
JavaScript

12 Comments(s) โ†ด

Copy linkTweet thisAlerts:
@phpnoviceApr 21.2005 โ€”ย This comparison will not work for form fields:

if (form.roomaji.value >= 0) {

The reason is because all form fields contain string values -- not numeric. You can do this:

if (Number(form.roomaji.value) >= 0) {
Copy linkTweet thisAlerts:
@balloonbuffoonauthorApr 21.2005 โ€”ย Thank you for the reply, however its stilling behaving the same. It will work for numbers 0-9, but not 10 or 100.

Thanks,

Steve
Copy linkTweet thisAlerts:
@phpnoviceApr 21.2005 โ€”ย Well, perhaps instead of using [b]onkeyup[/b], you should use [b]onchange[/b]. Keep in mind, though, that the [b]onchange[/b] event does not fire until the object loses ocus -- such as when you press the tab key or click on a button.
Copy linkTweet thisAlerts:
@phpnoviceApr 21.2005 โ€”ย ...or, perhaps this test:

if (form.roomaji.value >= 0) {

needs to be this:

if (form.roomaji.value.charAt( form.roomaji.value.length - 1) >= 0) {
Copy linkTweet thisAlerts:
@balloonbuffoonauthorApr 21.2005 โ€”ย Well onchange won't work for my application, but I tried it just to see what happens and still the same behavior. Also "form.roomaji.value.charAt( form.roomaji.value.length - 1)" should be the period at the end of the number so it wouldn't be comparable to 0. Thank you for your suggestions. This is just baffling me! :eek:

-Steve
Copy linkTweet thisAlerts:
@phpnoviceApr 21.2005 โ€”ย Well, here's the thing... Using [b]onkeyup[/b] means that your function gets control when the first character is entered. At that point, the entry isn't complete yet. Right? So, then what? Your function isn't supposed to do anything until the period is entered? In that case, reverse your tests:
<i>
</i>function number_conv(form) {
if (form.roomaji.value.charAt( form.roomaji.value.length - 1)==".") {
if (Number(form.roomaji.value) &gt;= 0) {
form.textarea.value+=digit[Number(form.roomaji.value)];
form.roomaji.value="";
}
}
}
Copy linkTweet thisAlerts:
@balloonbuffoonauthorApr 21.2005 โ€”ย Thanks again, but still no luck! Still only works with 0-9 and nothing else. But the way you rearranged it is more efficient, if I could just get the meat of the function working.

Thanks,

Steve
Copy linkTweet thisAlerts:
@phpnoviceApr 21.2005 โ€”ย I just tested it and it works perfectly for me. Why do you think it is not working?
[code=html]
<script type="text/javascript">
<!--//
var digit=new Array();
digit[0]="u3007";
digit[1]="u4E00";
digit[2]="u4E8C";
digit[3]="u4E09";
digit[4]="u56DB";
digit[5]="u4E94";
digit[6]="u516D";
digit[7]="u4E03";
digit[8]="u516B";
digit[9]="u4E5D";
digit[10]="u5341";
digit[100]="u767E";
function number_conv(form) {
if (form.roomaji.value.charAt( form.roomaji.value.length - 1)==".") {
if (Number(form.roomaji.value) >= 0) {
form.textarea.value+=digit[ Number( form.roomaji.value)];
form.roomaji.value="";
}
}
return true;
}
//-->
</script>
<form name='form'>
<input name="roomaji" type="text"
onkeyup="return number_conv(this.form)" size="25"><br>
<textarea name="textarea" cols="66" rows="25"></textarea>
</form>
[/code]
Copy linkTweet thisAlerts:
@balloonbuffoonauthorApr 21.2005 โ€”ย Well thank you. It does work alone (I can see that now), but I am using it with other functions. (I should have mentioned it, but I didn't think it would make a difference.) Here is the entire script: http://theballoonbuffoon.com/html_entities.php

The code is way to big to post here, so you can look at the source of the page. Thank you for all your help already.

-Steve
Copy linkTweet thisAlerts:
@phpnoviceApr 21.2005 โ€”ย If this is the correct intended nested structure of your code, then the blue code will never get executed when the period is beyond the second character because the red code will have taken it down a different path.
<i>
</i>function convert(form) {
if (hiragana[form.roomaji.value]!=undefined) {
form.textarea.value+=hiragana[form.roomaji.value];
form.roomaji.value="";
[COLOR=Red]} else if (form.roomaji.value.length &gt; 2) {[/COLOR]
var splits=form.roomaji.value.split("");
if (splits[0]==splits[1]) {
if (form.roomaji.value.length == 3) {
var actual=splits[1]+splits[2];
if (hiragana[actual]!=undefined) {
var enter="u3063"+hiragana[actual];
form.textarea.value+=enter;
form.roomaji.value="";
}
} else if (form.roomaji.value.length == 4){
var actual=splits[1]+splits[2]+splits[3];
if (hiragana[actual]!=undefined) {
var enter="u3063"+hiragana[actual];
form.textarea.value+=enter;
form.roomaji.value="";
}
}
}
[COLOR=Blue]} else if (form.roomaji.value.charAt( form.roomaji.value.length - 1)==".") {[/COLOR]
if (Number(form.roomaji.value) &gt;= 0) {
form.textarea.value+=digit[ Number( form.roomaji.value)];
form.roomaji.value="";
}
}
return true;
}
Copy linkTweet thisAlerts:
@balloonbuffoonauthorApr 21.2005 โ€”ย Thank you for pointing that out, while I was lying in bed I was thinking that might be the problem. Thanks for all your help (sorry I wasted your time! :o ) Everything is working like it should, I just moved the the blue chunk above the red chunk.

-Steve
Copy linkTweet thisAlerts:
@phpnoviceApr 21.2005 โ€”ย Yup, that be the solution. ?

Cheers.
ร—

Success!

Help @balloonbuffoon spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 โ€” BETA 5.4,
whats_new: community page,
up_next: more Davinciโ€ข003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinciโ€ข003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...