JavaScript Verification Number of 4 1 Format Digital Instance Code

  • 2021-08-03 09:05:33
  • OfStack

There is an entry in the project, which needs to be verified in digital format. The front end uses miniUI framework, and the miniUI document comes with its own verification. vtype= "float" verifies floating-point numbers, but it cannot verify that this floating-point number has several integers and several decimals, so it is necessary to rewrite an js function to verify:

Implementation ideas:

1. Get the values you fill in

2. Judge whether it is empty, and if it is not empty, execute 3

3. Remove spaces from the string trim (), and judge that the number starting or ending with "." is not a legal number, and give a hint.

4. Determine whether the string is true or false in advance, because the Number function will be used below, which can convert true and false to 1 and 0, so you need to make a judgment before using it. If the string is this, give a prompt

5. Use Number function to transform the string into a number. If the transformed value is 0, the original string is empty; If the converted number is NaN, the original string is not a pure numeric string. Give the corresponding prompt, and if it is a pure numeric string, execute 6

6. Change the transformed value to String type, first judge whether the length of the value is legal, give a prompt if it is illegal, and execute it legally 7

7. Judge whether the integer bit of the numerical value conforms to the definition of Number type, which is greater than the minimum value and less than or equal to the maximum value. If 8 is legally executed, give a prompt if it is illegal;

8. Judge decimal places, use substring () function, and if it is legal, return true; On the contrary, the corresponding error prompt is given.

Implementation code:


 // Calibration number(4,1)
  function check(v){
   var str = mini.get(v).getValue();
   // Non-empty 
   if(str!=null && str.length>0){
    str = str.trim();// Remove spaces 
    if(str.substring(0,1)=="." || str.substring(str.length-1)=="."){
     mini.alert(" Please enter a valid value ");
     return false;
    }
    // Exclude Boolean Value 
    if(str==true||str==false){
     mini.alert(" Please enter a valid value ");
     return false;
    }else {
     var num = Number(str);// Convert a string to a numeric type 
     if(num == 0){// The string entered is empty 
      mini.alert(" Please enter a valid value ");
      return false;
     }else if(isNaN(num)==true){// The input is a non-numeric string 
      mini.alert(" Please enter a valid value ");
      return false;
     }else {// Pure numeric character 
      num = num.toString();
      var numLenth = num.length;
      if (numLenth >5) {
       mini.alert(" Value exceeds effective length ");
       return false;
      }else{// Significant number 
       var pointIndex = num.indexOf(".");
       if (num.substring(0,pointIndex).length>3 || num.substring(0,pointIndex).length<=0) {// The integer part must be in the 1-3 Bit number 
        mini.alert(" The significant number length of integer part exceeds the valid range ");
        return false;
       }else if(pointIndex>0){// Judge the decimal part 
        if(num.substring(pointIndex).length>2 ||(num.substring(pointIndex).length==1)){
         mini.alert(" Decimal part is not valid ");
         return false;
        }
       }
      }
     }
    }
   }
   return true;
  }

Related articles: