JavaScript compares two numeric size issues

Source: Internet
Author: User

Today encountered a problem, in the JS comparison two values of the size, according to the normal logic write dead and alive will not work.
The problematic code is as follows:

JS Code
  1. function Chknum () {
  2. var appcount = $ ("#appcount"). Val ();
  3. var intencount = $ ("#intencount"). Val ();
  4. var unemployment = $ ("#unemployment"). Val ();
  5. if (appcount = = Null | | appcount = = "") {
  6. Alert ("The number of candidates cannot be empty!")  ");
  7. return false;
  8. }
  9. if (intencount = = Null | | intencount = = "") {
  10. Alert ("The initial number of recruits cannot be empty!")  ");
  11. return false;
  12. }
  13. if (Intencount < Appcount) {
  14. Alert ("Initial employment should be less than the number of applicants!");
  15. $ ("#intencount"). Val ("");
  16. return false;
  17. }
  18. if (unemployment > Appcount) {
  19. Alert ("The number of unemployed should be less than the number of applicants!");
  20. $ ("#unemployment"). Val ("");
  21. return false;
  22. }
  23. };



This problem tangled for a long time, the internet looked down JS incredibly have an eval function
eval function definition and usage
The eval () function computes a string and executes the JavaScript code in it.

The modified code

JS Code
    1. function Chknum () {
    2. var appcount = $ ("#appcount"). Val ();
    3. var intencount = $ ("#intencount"). Val ();
    4. var unemployment = $ ("#unemployment"). Val ();
    5. if (appcount = = Null | | appcount = = "") {
    6. Alert ("The number of candidates cannot be empty!")  ");
    7. return false;
    8. }
    9. if (intencount = = Null | | intencount = = "") {
    10. Alert ("The initial number of recruits cannot be empty!")  ");
    11. return false;
    12. }
    13. if (eval (intencount) > eval (appcount)) {
    14. Alert ("Initial employment should be less than the number of applicants!");
    15. $ ("#intencount"). Val ("");
    16. return false;
    17. }
    18. if (eval (unemployment) > eval (appcount)) {
    19. Alert ("The number of unemployed should be less than the number of applicants!");
    20. $ ("#unemployment"). Val ("");
    21. return false;
    22. }
    23. };

JavaScript compares two numeric size issues

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.