Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7937677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:34:50+00:00 2026-06-03T22:34:50+00:00

I have a function that checks if a user writes a digit in an

  • 0

I have a function that checks if a user writes a digit in an input box:

var letters = 'ABCÇDEFGHIJKLMNÑOPQRSTUVWXYZabcçdefghijklmnñopqrstuvwxyzàáÀÁéèÈÉíìÍÌïÏóòÓÒúùÚÙüÜ';
var numbers = '1234567890';
var signs = ',.:;@-\'';
var mathsigns = '+-=()*/';
var custom = '<>#$%&?¿';


function alpha(event) {
    var k;
    k = document.all ? parseInt(event.keyCode) : parseInt(event.which);
    return (numbers.indexOf(String.fromCharCode(k)) != -1);
}

This function is used together with the following function, that takes the digit and sends it to a controller, which saves the digit in the database. But only if the digit is different from the original digit, that was in the input box in the first place. It also updates another input box, with the sum off all specific input that have the same row_id attribute as the input box which fired the event:

function initializeUpdateNumberProcentMatrix() {

    $(".numberfield").live('keypress', alpha);

    //$(".numberfield").live('keypress', alpha(numbers));


        $('.numberfield').live('focus', function () {
            $(this).attr('oldvalue', $(this).val());
        });

        $('.numberfield').live('blur', function () {

            // Dette er den nuværende værdi i tekstboksen
            var value = $(this).val();
            var oldvalue = $(this).attr('oldvalue');

            // Reference til tekstboksen
            var obj = $(this);

            // Hvis værdien ikke har ændret sig, skal der ikke foretages noget (hvis man tabulerer)
            if (value == $(this).attr('oldvalue')) {
                return;
            }

            else {

                var dif = value - $(this).attr('oldvalue');
                var newval;


                $('.procentsumfield').each(function (index) {
                    if ($(this).attr('row-id') == obj.attr('row-id')) {

                        newval = (parseInt($(this).val()) + parseInt(dif));

                        // Hvis summen overstige 100, skal der ikke foretages nogle ændringer - Textboksens værdi skal tilbagestilles, og sumfeltet skal ikke odateres                
                        if (newval > 100) {
                            obj.val(oldvalue);
                            alert("Summen for det pågældende år må ikke overstige 100%");
                            return;
                        }
                        else {
                            $(this).val(newval);
                            return false;
                        }
                    }
                });

                var number = { Column_ID: $(this).attr('column-id'),
                    Row_ID: $(this).attr('row-id'),
                    Question_ID: $(this).attr('question-id'),
                    Value: $(this).val(),
                    Type: "Procent"
                };

                // Hvis den nye værdi overstiger 100, skal det ikke gemmes
                if (newval <= 100) {
                    saveNumberMatrix($(this), number, "/SaveSurveyAnswers/SaveProcentMatrix");
                }
            }
        });
    }

The problem is, that sometimes IE 7 gives me a NaN in the “procentsumfield” input box, and it doesnt save the new value because it’s not a number.

What could cause this to happen?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T22:34:52+00:00Added an answer on June 3, 2026 at 10:34 pm
    var dif = value - $(this).attr('oldvalue');
    

    Both value and $(this).attr('oldvalue') are strings. - only works on ints, so string-string is NaN.

    You need to use parseInt on these.

    var value = parseInt($(this).val(), 10);
    var oldvalue = parseInt($(this).attr('oldvalue'), 10);
    

    Note the ,10, this makes sure the values are “base 10”.

    EDIT: As stated in the comments, the NaN is coming from $(this).val() being empty (''). You can check for that like so:

    if($.trim($(this).val()) === '')
    

    Or:

    var value = parseInt($(this).val(), 10);
    if(isNaN(value))
    

    You set oldvalue but you never use it, you should use it instead of getting the attribute each time.

    var dif = value - oldvalue;
    

    Also change this line newval = (parseInt($(this).val()) + parseInt(dif)); to:

    newval = parseInt($(this).val(), 10) + dif;
    

    dif is already an int, no need for parseInt.

    Another note: In jQuery event.which is normalized (and should be an int), so you don’t need to “check” for it.

    function alpha(event) {
        return (numbers.indexOf(String.fromCharCode(event.which)) != -1);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function that checks user input and wanted to know if it
I have a log function on my admin panel that checks user input for
I have a function that checks if a cookie (by name) exists or not:
I have a javascript function that checks for a date range. Is there any
I have this code snippet inside a function that checks if an object exists
Let’s say I have a function that accepts a user name and password, it
Scenario: I have to check for user input (a string) that shouldn't contain <.%?/
I have this code that loads on document.ready and it checks if the user
I have a CustomAuthorize attribute that checks to see if a user has access
I have this simple code that speaks for itself. <script language='javascript"> function check() {}

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.