Jump to content

String to Uppercase Problem


Dummy99
Go to solution Solved by ahmedarain24,

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • Solution

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').

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.