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

Regex validation on integer field

richard38
Tera Guru

Hello, I'm trying add some validation to an integer field. The validation is for positive, whole numbers. I've found a regex example that works, but only for numbers 1-999.

      var regExTest = /^[1-9][0-9]*$/;      

      g_form.hideFieldMsg("u_regex_test", true);

     

      if(!regExTest.test(newValue)){

              g_form.setValue("u_regex_test", "");

              g_form.showFieldMsg("u_regex_test","Enter a valid integer", "error");      

      }

My understanding is that the '*' represents the number of items being validated, similar to adding {0, } for min/max numbers. I've tried this in various online regex validators and they all show that this validates correctly, but it doesn't work as anticipated. Am I missing something?

thank you

11 REPLIES 11

richard38
Tera Guru

Thanks everyone. What seems to be happening is when the newValue is entered, and is four digits, it now has a comma. Once I included the comma in the regex it worked.



This works:


var regExTest = /^[1-9][0-9,]*$/;


Sudeepta
Tera Contributor

If you only want to restrict the field value as Integer , then you can try below script. it worked for me.




function onChange(control, oldValue, newValue, isLoading, isTemplate) {


    if (isLoading || newValue == '') {


          return;


    }




var regexp =/^\d+$/;



  if(!regexp.test(newValue))


  {


    alert('Please enter Numbers. Only numbers are allowed');


  g_form.setValue('space_added','');


  }


}



N.B :   "space_added" is the field name.



HIt Like when you find this useful.