NetTalk Central

Author Topic: Entry picture tokens  (Read 4841 times)

Devan

  • Full Member
  • ***
  • Posts: 230
    • View Profile
    • Email
Entry picture tokens
« on: December 20, 2011, 04:20:10 AM »
Hi all,

I know that you can use standard Clarion picture tokens to display data in Browses (e.g. @N10.2) but can you do the same to validate data entry?

I have a form where the user should only be able to type in a whole number, but at present, they can type decimals too.  Any way to restrict this and validate when they try to move off the field?  Even better, can we prevent them from typing a '.' or any other non numeric character in that field?

Thanks,
Devan

Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 11250
    • View Profile
Re: Entry picture tokens
« Reply #1 on: December 20, 2011, 07:08:20 AM »
you can, but you'd need to use javascript to do it.
First though, make sure you get the server-side right (ie server-side validation.)
You can't "trust" client side validation, so that should only come after you've made sure the server side is ok.

cheers
Bruce

terryd

  • Hero Member
  • *****
  • Posts: 759
    • View Profile
    • Davcomm
    • Email
Re: Entry picture tokens
« Reply #2 on: December 20, 2011, 08:18:04 PM »
Devan a suggestion. Maybe let them enter the decimals but do something like p_web.SSV('fieldname',INT(p_web.GSV('fieldname')) in the server side code for the affected fields?
Terry Davidson
Windows 10 64 bit/Windows7 64bit
Clarion 9.1.11529/Clarion10 12567
Nettalk 913
Nettalk 1015
StringTheory267/Winevent515/XFiles298/MessageBox239/Cryptonite186

Devan

  • Full Member
  • ***
  • Posts: 230
    • View Profile
    • Email
Re: Entry picture tokens
« Reply #3 on: December 20, 2011, 08:40:34 PM »
Thanks for the reply Bruce.

Terry - Haha, that is the way that I ended up doing it finally.  The client expressed a preference for 'no decimal places' to be entered, but are happy if the field magically converts to an integer field as soon as the user tabs off it.

I saw this code snippet on the netz which would (I think) solve my original issue of 'real time' prevention, but alas, I don't think there is a way I can embed other javascript event handling code in the <input> control?

Code: [Select]
<input type="text" onkeyup="this.value=this.value.replace(/[^0-9]/,'')">
Bruce - perhaps incorporate a prompt in the templates for adding attributes to the form fields?  ;)

Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 11250
    • View Profile
Re: Entry picture tokens
« Reply #4 on: December 20, 2011, 11:57:58 PM »
I think at some point in time it would make some sense to do a webinar or training or something on JavaScript - and especially the way NetTalk uses JavaScript, and the way jQuery makes the JavaScript easier.

For example, here's how you would add this bit of JavaScript to your form. (while it's position is almost immaterial, it makes sense to add this in the last embed in the Value::FieldName routine. (This is all one line)

p_web.script('$("#MAI__SizeLimit").off("keyup.nt").on("keyup.nt",function(){{this.value=this.value.replace(/[^0-9]/,"")})')

since this is the key to pretty much all JavaScript power, it's worth breaking down to see what's happening.
Note that for convenience I use double quotes (") in the javascript code rather than single quotes, as it makes the Clarion code easier to read. Also note the requirement for a {{ instead of a { - this is a clarion string convention. The JavaScript of course has but one {.

So firstly the p_web call

p_web.Script(string)

This call works in both page mode, and ajax mode, and so simplifies all the work you would typically need to do to handle both modes.While there are other optional parameters to the Script method, the vast majority of cases just use the one parameter.

The JavaScript is of course the "string" parameter, which in this case is

$("#MAI__SizeLimit").off("keyup.nt").on("keyup.nt",function(){{this.value=this.value.replace(/[^0-9]/,"")})


let's reformat that a bit to make it easier to read

$("#MAI__SizeLimit")
      .off("keyup.nt")
      .on("keyup.nt",function(){{
          this.value=this.value.replace(/[^0-9]/,"")
       }
  )


The first line $("#someid") identifies the element(s) to which this bit of code will be attached. The "#someid" is known as a "selector" and is just absurdly powerful. Feel free to read http://api.jquery.com/category/selectors/ for more on selectors. You will honestly weep when you realize how staggeringly elegant, and powerful selectors are. If jQuery has two core bits of magic, the first is the whole idea of using selectors.

The second core magic is the idea that method calls can be "chained" together. So starting with a selector you can

.dothis().thenthat().thensomethingelse()

Each of these 3 methods are called in turn, on the same "collection" of elements. this is the same as

$("selector").dothis();
$("selector").thenthat();
$("selector").thensomethingelse();

but takes less code, and is faster.

In the actual code above, two methods are being called, the first called off, the next called on.

The second and third lines are related. "off " is the opposite of "on" (duh) and removes any previous things, with the same name, that you may have hooked. Because NetTalk pages are very dynamic, it's good practice to do an off before an on.

Both take an event as the first parameter. In this case we're looking for a keyup event. We've narrowed the scope by adding a random, arbitrary, "extension" to the event name. This allows the "off" to be very specific. The .nt part of keyoff.nt is just "made up" to narrow the scope of the off to that very specific on.

The second parameter to "on" is a simple JavaScript function (which I lifted from your code. The only change I made was using double quotes instead of single quotes.)

JavaScript, and jQuery are not hard to use, once you get the basic idea, and the most powerful things are usually quite straight-forward to code. Wrapping them in a p_web.Script call in turn make it easy to embed the JavaScript on dynamic web page.

« Last Edit: December 21, 2011, 12:09:13 AM by Bruce »

Devan

  • Full Member
  • ***
  • Posts: 230
    • View Profile
    • Email
Re: Entry picture tokens
« Reply #5 on: December 21, 2011, 04:37:40 AM »
Bruce,

Thanks for the lengthy and VERY informative post!  I had a little bit of an idea about jQuery selectors from other projects, but I had NO idea at all that I could use p_web.Script() to embed js scripting within my NetTalk pages...

Very very interesting indeed...this opens up a whole new world!  :o

Cheers,
Devam