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

  • Home
  • SEARCH
  • 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 6216397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:14:38+00:00 2026-05-24T07:14:38+00:00

I have the following function: function validateField(target, field, field_check) { if(target.val().trim().length > 0) {

  • 0

I have the following function:

function validateField(target, field, field_check) {
            if(target.val().trim().length > 0) {
                $.ajax({
                    url: '<?php echo $html->url('/fonykers/validate_',true); ?>'+ field + '/' + target.val(), 
                    dataType: 'json',
                    type: 'POST',
                    success: function(response) {
                        if(!response.ok) {
                            if(target.is('.ok')) {
                                target.removeClass('ok');
                            }
                            target.addClass('error');
                            error.html(response.msg);
                            error.fadeIn();
                            field_check = false;
                        } else {
                            if(target.is('.error')) {
                                target.removeClass('error');
                            }
                            target.addClass('ok');
                            alert('voy a asignar true');
                            field_check = true;
                        }
                    },
                        error:function (xhr, ajaxOptions, thrownError){
                            alert(xhr.statusText);
                            alert(thrownError);
                            field_check = false;
                        } 
                });        
            } else {
                error.html('You must specify a ' + field);
                error.fadeIn();
                target.addClass('error');
                field_check = false;
            } 

        }

I use it to validate my form input fields, the target variable es the field itself, for instance: $('#name_field') the 2nd variable es a string specifying the name of the field for instance name as to know what PHP function it’s gonna call. The last is a boolean that I use to know wether or now the field is valid or not for instance name_check which I define at the beginning of the scripta and set to false.

My problem is that field_check gets changed in the local scope properly but it never modifies que external variable that has been passed in, what am I doing wrong?

  • 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-05-24T07:14:38+00:00Added an answer on May 24, 2026 at 7:14 am

    Primitive variables (int / boolean / etc) are passed to functions by value – that is, when someone passes one of them to a function, the value of the variable is passed to your function, not the variable itself. What you’re modifying is in fact a different variable altogether.

    The easiest solution is to just return true or false when you’ve finished validating the field, and use the function’s return value instead of checking field_check.

    You can also do a couple different things with globals / properties. For instance…

    var validator = function (target, field) {
      ...
      this.field_check = false;
    }
    
    validator(target, field);
    if (validator.field_check) {
      ...
    }
    

    Note the use of this.field_check and the assignment of the function to a variable

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have following function: function fnGetIdentifiers(id){ $.get( 'getCultureIdentifiers.php?cultureID=' + id, function(data){ if (data.length >
I have the following function that is pulling data from a database. The ajax
I have the following function: function validateField(query) { new Request.JSON({ onRequest : function() {
I have the following function where I attempt to load a specified url into
i have following function in php, how can i convert this in javascript? function
Suppose we have following function in PHP: public function testSomething() { $name = perform_sql_query("SELECT
I have the following function to make a popup window. function makewindows(html){ child1 =
I have the following function: <?php class Test{ function myFunction(){ return array('first','second','third'); } }
I have the following function: CREATE FUNCTION fGetTransactionStatusLog ( @TransactionID int ) RETURNS varchar(8000)
I have the following function: //Function to get random number public static int RandomNumber(int

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.