/    Sign up×
Community /Pin to ProfileBookmark

Alert if & is entered

I have a ajax page that saves the data after each button is pressed, aka, onChange event:

[code]
function SaveRecord(strFieldName, intLineSerial, strValue)
{
var url=”Order_Entry_Detail-save.asp?Value=” + strValue + “&UpdateDetail=Yes&FieldName=” + strFieldName + “&LineSerial=” + intLineSerial

xmlHttp=GetXmlHttpObject(stateChangedSaveRecord)
xmlHttp.open(“GET”, url , true)
xmlHttp.send(null)
}
[/code]

If the & key is pressed, I wish to have an alert before the var url= line so I can exit teh function before it goes to the save and errors out. Any ideas on how to accomplish this?

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@ZeroKilledApr 30.2008 — i had never worked with ajax technology, but i guess with a simple if condition you can verify the entered string. i assume the argument strValue is used for the typed string. if not, replace it with the proper argument:
<i>
</i>if(/&amp;/.test(strValue))return false;

add this line as the first line of the block function. if an & is typed, it will return false. you can return whatever value you want, an error, boolean, string or number. whatever you think is helpful for you.
Copy linkTweet thisAlerts:
@dzirkelbauthorApr 30.2008 — That works great. I think I want to go even further though and add it to this function also:

<i>
</i>function enter(NextField, NextFieldDown, NextFieldUp)
{
if(window.event &amp;&amp; window.event.keyCode == 40) {
NextFieldDown.focus();}

<i> </i>if(window.event &amp;&amp; window.event.keyCode == 38) {
<i> </i> NextFieldUp.focus();}

<i> </i>if(window.event &amp;&amp; window.event.keyCode == 13) {
<i> </i> NextField.focus();
<i> </i> return false; }
<i> </i>else
<i> </i> return true;
}


This runs something on the keydown portion, and I think I want it to happen there also. What should I place in here for the window.event.keycode?
Copy linkTweet thisAlerts:
@toicontienApr 30.2008 — This is more difficult. The "&" character is the "7" key on most keyboards. Simply testing window.event.keyCode will always show the key code for the "7" key without you knowing the use is pressing the SHIFT key also (thus entering "&" instead of "7"). You'll need to create a function that knows when the user has pressed the SHIFT key, and keep track of whether or not the SHIFT key is down or up. I've done it before and it's kind of a PITA. That should be my next small project for JavaScript. ?
Copy linkTweet thisAlerts:
@toicontienApr 30.2008 — Well, I think I've got something working. I've attached a ZIP file containing a JavaScript file and an example HTML file implementing it. Reply back if you have any questions. It only keeps track of the CTRL, SHIFT and ALT keys. There are some browser issues that make this a somewhat unreliable script. See the comments in the JavaScript file for more information.

But this is about as good as it gets.

The HTML file shows how you can use it.

[upl-file uuid=965c71a4-dee0-4639-b360-f30c8fabcb6d size=2kB]KeyState.zip[/upl-file]
Copy linkTweet thisAlerts:
@dzirkelbauthorMay 01.2008 — Thanks! I'll play around with this ?
×

Success!

Help @dzirkelb 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 6.17,
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: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

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