The CreatorCon Call for Content is officially open! Get started here.

Convert Number text to actual number field

asher14
Tera Contributor

in there, num is passed in but I'm guessing it wasn't working, because num isn't actually a "number" field, it's a "text" field that contains all numbers.
so I need to convert num from a text into an actual number, before that if(!pattern.test(num)... line hits. How can I do that?

Things I've tried so far in the client script: 

added in the function

num = num.replace(/,/g,''); //remove the commas
var numAsNumber = parseFloat(num); //try to parse the input as floating-pt number
var pattern = new RegExp("[^0-9]"); //removed the comma after 9

HTML

<div class="form-group">
<label for="phone">Phone</label>
<input id="phone" name="phone" ng-model="c.Form.data.u_phone" type="text" class="form-control" placeholder="Number..." ng-readonly="!c.Form.editing" ui-validate="{numbersOnly: 'c.validation.fields.numbersOnly($value, c.Form.data)'}" ui-validate-watch="'c.Form.data.u_phone'" maxlength="500">
</div>
</div>

 

CLIENT SCRIPT

c.validation = {

fields: {

numbersOnly : function(num, equipment) {

var pattern = new RegExp("[^0-9,]");

var test = !pattern.test(num);

if(!pattern.test(num) || (num === undefined))

return true;

return false;

},

1 REPLY 1

Saurabh Gupta
Kilo Patron

Hi,

You can use the method parseInt(num) for the same.

 

 


Thanks and Regards,

Saurabh Gupta