Dummy99 Posted May 29, 2023 Share Posted May 29, 2023 My code doesn't work if I put in the uppercase statement, i.e., it accepts any character. If I leave the uppercase statemnt out, my code works. It seems to me my statement is correct. {newent.value = strtoupper(newent.value) if(newent.value == 'B' || newent.value == 'P' || newent.value == 'T' || newent.value == 'D' ) {EnterSw.value=0;return true;} else{alert('INVALID HAND / MAIN INVALID');newent.select();return false;}} Thanks for your help. Quote Link to comment https://forums.phpfreaks.com/topic/316354-string-to-uppercase-problem/ Share on other sites More sharing options...
requinix Posted May 29, 2023 Share Posted May 29, 2023 strtoupper is a PHP function. You are using Javascript. They are completely different things. newent.value is (presumably) a string. This page may be helpful to show you what you can do with it. Quote Link to comment https://forums.phpfreaks.com/topic/316354-string-to-uppercase-problem/#findComment-1608718 Share on other sites More sharing options...
Dummy99 Posted May 29, 2023 Author Share Posted May 29, 2023 Sorry. No insult to PHP intended. I had been told it was PHP code. Many thanks for you help. Quote Link to comment https://forums.phpfreaks.com/topic/316354-string-to-uppercase-problem/#findComment-1608722 Share on other sites More sharing options...
ginerjm Posted May 30, 2023 Share Posted May 30, 2023 I offer this correction to your code. It makes it easier to read and is better in the long run to learn to code this way. you have: if(newent.value == 'B' || newent.value == 'P' || newent.value == 'T' || newent.value == 'D' ) {EnterSw.value=0;return true;} else{alert('INVALID HAND / MAIN INVALID');newent.select();return false;}} This would be much better: if(newent.value == 'B' || newent.value == 'P' || newent.value == 'T' || newent.value == 'D' ) { EnterSw.value = 0; return true; } else { alert('INVALID HAND / MAIN INVALID'); newent.select(); return false; } Note how I inserted the lines within the {} under the if and under the else. Also - it is better to only put one statement per line to make it easier to find statements when you are developing that may be causing you problems. Joining them into one line makes them harder to find. Quote Link to comment https://forums.phpfreaks.com/topic/316354-string-to-uppercase-problem/#findComment-1608736 Share on other sites More sharing options...
Psycho Posted May 31, 2023 Share Posted May 31, 2023 Try this newent = newent.toUpperCase(); Quote Link to comment https://forums.phpfreaks.com/topic/316354-string-to-uppercase-problem/#findComment-1609318 Share on other sites More sharing options...
Solution ahmedarain24 Posted June 1, 2023 Solution Share Posted June 1, 2023 Based on the code you provided, it seems that you are trying to convert the value of `newent` to uppercase using the `strtoupper()` function. However, it appears that the uppercase conversion is not working as expected and accepting any character. To properly convert the value to uppercase in JavaScript, you should use the `toUpperCase()` method instead of `strtoupper()`. Here's the corrected code: ```javascript newent.value = newent.value.toUpperCase(); if (newent.value === 'B' || newent.value === 'P' || newent.value === 'T' || newent.value === 'D') { EnterSw.value = 0; return true; } else { alert('INVALID HAND / MAIN INVALID'); newent.select(); return false; } ``` With this modification, the `toUpperCase()` method will convert the value of `newent` to uppercase, and the subsequent comparisons will check against uppercase characters ('B', 'P', 'T', 'D'). Quote Link to comment https://forums.phpfreaks.com/topic/316354-string-to-uppercase-problem/#findComment-1609322 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.