Let's follow up this discussion at the Neptune Community

(this forum will be kept in read-only mode)

Currency and Amount validation

Hi All


Was wondering how you handle currency and amount validations.


I have an input box bound to a currency field, I set the format for sap default currency but it allows me to type alpha characters into the input box.


Do you have a library or process that handles currencies amounts and dates


Many thanks


Hi Darren,


Unfortunately, we do not have a library for currency validation.  At this moment I can think of two quick options:


1 - Validating in the ABAP class against TCURC table.

2 - In the UI, offering 2 separate fields:  one for the currency and one for the amount.

The field for the currency could be a dropdown with the "valid currencies for your scenario" so that the user has to choose between the valid values.


For dates, we normally use the Datepicker component so that valid dates are entered by the user.  For extra validation then you would have to code some javascript.

Kind regards,

Julian

Hi Julian


Thanks for the quick reply.  I should have been clearer in my request.


I need to prevent the user from entering un-wanted characters at the ui layer.  In a currency field I need the user to enter e.g. 123.50 not aBdcdl i.e alpha chars.


Thanks


Hi


Something like the sap.m.MaskInput component..

See

https://openui5beta.hana.ondemand.com/#docs/guide/99ac68a5b1c3416ab5c84c99fefa250d.html

You are welcome, Darren.


I think a quick solution could be setting the sap.m.Input field to Numeric


Hi Juliian


This will still allow alpha chars to be entered, I am testing on desktop


Thanks

Oh, that was strange.  It did work here (Chrome on desktop).


Ok, this is bulletproof:  


In the liveChange event of your sap.m.input field, paste the following:


var str = this.getValue();

var ret = '';


// Check each char

for (i = 0; i < str.length; i++) {


    if (isNumber(str[i])) {

        ret = ret + str[i];

    } else {

        console.log(str[i] + " is not a number");

    }

}

this.setValue(ret);


In one of your Script Codes, you also need to declare the following function:


function isNumber(str) {

var pattern = /^\d+$/;

return pattern.test(str);} // returns a boolean



Hi Julian


Works on Chrome but not on IE11


Thanks for the code.


Have you used the mask attribute on sap.m.input ?  Doesn't seem to do anything.

You are welcome.    Our best friend IE11  :)

I guess you will have to add an extra check for IE11 (onChange and before submitting the value).

I have not used the mask attribute before.


Thanks and regards,

Julian

I have a work around for anyone using IE for this.


In the live change event put the following code


var value = oInput.getValue( );


var valueState = isNaN(value) ? "Error" : "Success";

if (valueState == 'Error'){

  oEvent.getSource().setValueState(valueState);

}

else{

    oEvent.getSource().setValueState('None');

}

Cool !! thanks for sharing it

Login or Signup to post a comment