← Back to team overview

dhis2-devs team mailing list archive

Re: [Branch ~dhis2-devs-core/dhis2/trunk] Rev 4611: added proper validation for real/natural numbers (including support for e-notation)

 

Hello Morten,

I agree with these validation on field but why don't you apply the "Math
methods" - which are in commons - into them. The "Math methods" are working
on regex that I think it is very very robust in validating.

Btw, the name of method "positive/negative_natural_
<goog_1126764391>number<http://en.wikipedia.org/wiki/Natural_number>"
is quite sensitive to me.

On Mon, Sep 19, 2011 at 3:41 AM, <noreply@xxxxxxxxxxxxx> wrote:

> ------------------------------------------------------------
> revno: 4611
> committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
> branch nick: dhis2
> timestamp: Sun 2011-09-18 22:39:31 +0200
> message:
>  added proper validation for real/natural numbers (including support for
> e-notation)
> modified:
>
>  dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/i18n/messages.vm
>
>  dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/jQuery/jquery.validate.ext.js
>  dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/macros.vm
>  dhis-2/dhis-web/dhis-web-commons/src/main/resources/i18n_global.properties
>
>
> --
> lp:dhis2
> https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk
>
> Your team DHIS 2 developers is subscribed to branch lp:dhis2.
> To unsubscribe from this branch go to
> https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription<https://code.launchpad.net/%7Edhis2-devs-core/dhis2/trunk/+edit-subscription>
>
> === modified file
> 'dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/i18n/messages.vm'
> ---
> dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/i18n/messages.vm
>        2011-09-12 06:19:59 +0000
> +++
> dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/i18n/messages.vm
>        2011-09-18 20:39:31 +0000
> @@ -68,5 +68,8 @@
>                ,greaterDate:
> '$encoder.jsEscape($i18n.getString('closedDate_should_be_greater_than_openDate'
> ) , "'")'
>                ,unicodechars:
> '$encoder.jsEscape($i18n.getString('please_unicode_chars_only' ) , "'")'
>                ,unrecognizedcoordinatestring:
> '$encoder.jsEscape($i18n.getString('unrecognized_coordinate_string' ) ,
> "'")'
> -
> +               ,real_number:
> '$encoder.jsEscape($i18n.getString('please_enter_valid_real_number' ) ,
> "'")'
> +               ,natural_number:
> '$encoder.jsEscape($i18n.getString('please_enter_valid_natural_number' ) ,
> "'")'
> +               ,positive_natural_number:
> '$encoder.jsEscape($i18n.getString('please_enter_valid_positive_natural_number'
> ) , "'")'
> +               ,negative_natural_number:
> '$encoder.jsEscape($i18n.getString('please_enter_valid_negative_natural_number'
> ) , "'")'
>  };
>
> === modified file
> 'dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/jQuery/jquery.validate.ext.js'
> ---
> dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/jQuery/jquery.validate.ext.js
>       2011-08-15 14:42:49 +0000
> +++
> dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/jQuery/jquery.validate.ext.js
>       2011-09-18 20:39:31 +0000
> @@ -336,9 +336,52 @@
>     params[0] = (params[0] == '') ? new RegExp(params[0]) : params[0];
>
>     return this.optional(element) || params[0].test(value);
> -
>  });
>
> +jQuery.validator.addMethod("real_number", function(value, element, param)
> {
> +    value = +value;
> +
> +    if( isNaN( value ) )
> +    {
> +        return false;
> +    }
> +
> +    return true;
> +}, "Please enter a valid real number.");
> +
> +jQuery.validator.addMethod("natural_number", function(value, element,
> param) {
> +    value = +value;
> +
> +    if( isNaN( value ) || (""+value).indexOf('.') != -1 )
> +    {
> +        return false;
> +    }
> +
> +    return true;
> +}, "Please enter a valid natural number.");
> +
> +jQuery.validator.addMethod("positive_natural_number", function(value,
> element, param) {
> +    value = +value;
> +
> +    if( isNaN( value ) || (""+value).indexOf('.') != -1 )
> +    {
> +        return false;
> +    }
> +
> +    return value >= 0;
> +}, "Please enter a valid positive natural number.");
> +
> +jQuery.validator.addMethod("negative_natural_number", function(value,
> element, param) {
> +    value = +value;
> +
> +    if( isNaN( value ) || (""+value).indexOf('.') != -1 )
> +    {
> +        return false;
> +    }
> +
> +    return value <= 0;
> +}, "Please enter a valid negative natural number.");
> +
>  // Support method for date
>  //Parse a string and convert it to a Date object.
>  //If string cannot be parsed, return null.
>
> === modified file
> 'dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/macros.vm'
> --- dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/macros.vm
>      2011-09-18 19:46:53 +0000
> +++ dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/macros.vm
>      2011-09-18 20:39:31 +0000
> @@ -240,7 +240,7 @@
>  <tr>
>        <td style="width: 200px;">$!args.text</td>
>        <td>
> -               <input type="text" id="$!args.id" name="$!name"
> value="$!args.value"
> class="{validate:{required:$args.mandatory,rangelength:[2,230],number:true}}"
> style="width: 240px;" />
> +               <input type="text" id="$!args.id" name="$!name"
> value="$!args.value"
> class="{validate:{required:$args.mandatory,real_number:true}}" style="width:
> 240px;" />
>        </td>
>  </tr>
>  #end
> @@ -264,7 +264,7 @@
>  <tr>
>        <td style="width: 200px;">$!args.text</td>
>        <td>
> -               <input type="text" id="$!args.id" name="$!name"
> value="$!args.value"
> class="{validate:{required:$args.mandatory,rangelength:[2,230],digits:true}}"
> style="width: 240px;" />
> +               <input type="text" id="$!args.id" name="$!name"
> value="$!args.value"
> class="{validate:{required:$args.mandatory,natural_number:true}}"
> style="width: 240px;" />
>        </td>
>  </tr>
>  #end
> @@ -288,7 +288,7 @@
>  <tr>
>        <td style="width: 200px;">$!args.text</td>
>        <td>
> -               <input type="text" id="$!args.id" name="$!name"
> value="$!args.value"
> class="{validate:{required:$args.mandatory,rangelength:[2,230],digits:true}}"
> style="width: 240px;" />
> +               <input type="text" id="$!args.id" name="$!name"
> value="$!args.value"
> class="{validate:{required:$args.mandatory,positive_natural_number:true}}"
> style="width: 240px;" />
>        </td>
>  </tr>
>  #end
> @@ -312,7 +312,7 @@
>  <tr>
>        <td style="width: 200px;">$!args.text</td>
>        <td>
> -               <input type="text" id="$!args.id" name="$!name"
> value="$!args.value"
> class="{validate:{required:$args.mandatory,rangelength:[2,230],digits:true}}"
> style="width: 240px;" />
> +               <input type="text" id="$!args.id" name="$!name"
> value="$!args.value"
> class="{validate:{required:$args.mandatory,negative_natural_number:true}}"
> style="width: 240px;" />
>        </td>
>  </tr>
>  #end
>
> === modified file
> 'dhis-2/dhis-web/dhis-web-commons/src/main/resources/i18n_global.properties'
> ---
> dhis-2/dhis-web/dhis-web-commons/src/main/resources/i18n_global.properties
>  2011-09-16 07:53:59 +0000
> +++
> dhis-2/dhis-web/dhis-web-commons/src/main/resources/i18n_global.properties
>  2011-09-18 20:39:31 +0000
> @@ -376,6 +376,11 @@
>  unrecognized_coordinate_string = Unrecognized coordinate string
>  please_enter_a_letters_or_digits = Please enter letters or digits
>
> +please_enter_valid_real_number = Please enter a valid real number.
> +please_enter_valid_natural_number = Please enter a valid natural number.
> +please_enter_valid_positive_natural_number = Please enter a valid positive
> natural number.
> +please_enter_valid_negative_natural_number = Please enter a valid negative
> natural number.
> +
>  please_enter_name              = Please enter name!
>  please_select_period        = Please select period!
>  please_select_dataset       = Please select dataset!
>
>
> _______________________________________________
> Mailing list: https://launchpad.net/~dhis2-devs
> Post to     : dhis2-devs@xxxxxxxxxxxxxxxxxxx
> Unsubscribe : https://launchpad.net/~dhis2-devs
> More help   : https://help.launchpad.net/ListHelp
>
>


-- 
"*Expert By Chance*"

Follow ups

References