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

Deepa Srivastav
Kilo Sage

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



Please check links in the below blog also..



Let's learn a little about Regular Expressions...


Re: INteger only field



Mark correct if your issue is solved..


Thanks Deepa, but this does not work for me.


its working fine at my end. Can you provide screenshot .Try below on change client script



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


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


return


}


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



  if(!regexp.test(newValue)){  


  alert("Please enter valid characters");     g_form.setValue('urfieldname', '');  




  }  




}  


Abhinay Erra
Giga Sage

Hi richcab



Here you go



if(/\D/g.test(newValue)){


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


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


      }



RegExp \D Metacharacter