External JavaScript script files are causing issues, whereas internal scripts are functioning properly

Currently, I am working on incorporating JavaScript validations for mandatory fields within a form located in an ASP.NET Web Application.

The process involves obtaining the ClientID of the control that requires validation, extracting its value, and checking if it is empty:

function validate()
{
    if(document.getElementById('<%=textbox1.clientID %>').value=="")
    {
        alert('This field is mandatory');
        return false;
    }
}

I have successfully tested this validation logic when placed directly within the page and triggered upon a button click event.

However, moving the code to an external JavaScript file resulted in unexpected behavior. The function does not execute as intended and instead throws an error message stating 'object not found at document.getElementByid() line'.

Answer №1

Consider enhancing your method by including a parameter. When calling an external method, make sure to add the '<%=textbox1.clientID %>' parameter as shown below.

<javascript>
     var param = document.getElementById('<%=textbox1.clientID %>').value;
     validate(param);
</javascript>

Answer №2

Your code is making a call to a server-side property called textbox1.ClientID in JavaScript. To move this code into a .js file, you need to first reference this server-side property in your .aspx file and then pass the retrieved value to your JavaScript function:

function callValidation() {
    return validation('<%= textbox1.ClientID %>');
}

Subsequently, your .js file should include the actual validation logic:

function validate(elementId)
{
    if(document.getElementById(elementId).value=="")
    {
        alert('This field is mandatory');
        return false;
    }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

When redirecting to a .scala.html file in Scala Play, only text is displayed, not the full

I recently set up the Google OAuth hybrid server-side flow, which involves making an AJAX call to retrieve an access token. Upon successfully storing the token, I want the server to display content using a file named home.scala.html. // This function is c ...

"Encountered a floating-point issue when trying to read an Excel file with

When a user uploads an Excel file that contains decimal, string, and Unicode characters, I am encountering an issue with floating point errors when reading certain decimal values. For instance, a number like 0.15 is being read as 0.150000000002 in some c ...

Using an alias to call a function defined in a separate module in TypeScript

The following code snippet is from the v4.js file located inside the uuid folder within Angular's node_modules: var rng = require('./lib/rng'); var bytesToUuid = require('./lib/bytesToUuid'); function v4(options, buf, offset) { ...

Using the # symbol alongside other characters or numbers may prevent Reg Ex from validating

I am asking the same question again as I did before. I apologize, but I have checked all the links provided and they were not helpful. Also, this is my first time asking a question here so I was not very clear on how to ask. Here are the details of my iss ...

Enhance your web form with the Bootstrap 4 tags input feature that allows users to add tags exclusively

I'm currently utilizing Bootstrap 4 along with Bootstrap Tags Input to create a multiple category selection feature. My goal is to enable users to choose multiple items exclusively from the predefined categories listed in the predefined_list. At pres ...

What is the method for retrieving a gzip file using jQuery?

I have set up my own HTTP server using node.js and express.js. var express = require('express'); express().use(express.static(__dirname)).listen(3000); Within my static content folder, I have two test files: myfile.csv and myfile.csv.gz. These ...

What could be causing the error message "Why is property 'value' not found in null?" to appear in my code?

I keep encountering the "Cannot find property 'value' of null" error on line 2 of my JavaScript code. Despite entering text in the text-box, I am unable to resolve this issue. Is there a solution to fix this problem? HTML: <!DOCTYPE html> ...

Comparing Selenium and Watir for JavaScript Testing within a Rails environment

In our experience with Rails apps, we have found success using RSpec and Cucumber. While Webrat is effective for non-AJAX interactions, we are now gearing up to write tests for our Javascript. We have used Selenium support in Webrat before, but I am inter ...

Maintaining the original layout upon refreshing the page

Incorporating two forms on my website, I added a button that transitions between the login and sign-up forms smoothly. The process is as follows: Initially, the login form is displayed; clicking the button switches to the sign-up form. Proceeding to subm ...

Why does Res.send return an empty object when console.log indicates it is not empty?

I am currently facing a challenge while using the Google Sheets API with Express, as I have limited experience with JavaScript. My goal is to pass a JSON object from Express to React, but for some reason, when I send the object, it appears empty on the fro ...

Utilizing Animated GIFs as Textures in THREE.js

I'm trying to figure out how to incorporate a GIF animation as a texture in THREE.js. While I can successfully load a texture (even in GIF format), the animation doesn't play. Is there a solution to this? I came across some resources like these: ...

I am experiencing excessive paper skipping in my printer

I have been using the 80 column dot matrix printer. However, after each printout, the paper skips two times resulting in a lot of wasted paper. How can I resolve this issue? Currently, I am only utilizing the window.print() JavaScript function. Are there ...

What is the optimal placement for promises in Angular: Factory or Controller?

In my application, I have a basic factory to manage API calls. Currently, the structure looks like this: .factory('apiFactory', function($http){ var url = 'http://192.168.22.8:8001/api/v1/'; return { getReports: function() { ...

Are there any security risks in transmitting a password over HTTPS using jsonp?

Is it secure to send a password in JSONP using jquery over HTTPS for authentication if I can't use a JSON POST? EDIT: $.ajax({ type : "POST", url: "https://example.com/api.php", dataType: "jsonp", jsonp: "callback", data: { ...

Exploring location-based services using React-Redux

Seeking a deeper comprehension of redux and the react lifecycle methods. The issue I am facing involves a prop function within the componentDidMount that calls another function in redux. Within redux, I attempt to retrieve location data to set as the init ...

Establish a buffering system for the <video> element

Currently, I am facing an issue with playing videos from a remote server as they take an extended amount of time to start. It appears that the entire video must be downloaded before playback begins. Is there a way to configure the videos so they can begi ...

Is there a way to implement a scrollbar that only scrolls through one specific column in an HTML table?

I need help adding a scrollbar to a specific column in an HTML table. Take a look at this scenario https://jsfiddle.net/6wpdc4tL/: https://i.stack.imgur.com/svzIg.png This table should have two scrollbars, one for the light blue SCROLL column and another ...

What could be the reason behind the malfunction of the sideNav function in bootstrap?

I am trying to implement a Bootstrap navbar, but it's not displaying correctly. I keep getting an error in my console that says: https://i.sstatic.net/UwbAS.png I've rearranged the order of my scripts in the head section, but I still can't ...

Using <Redirect/> in ReactJS results in rendering of a blank page

Hello everyone, I've been working on a feature where I want to redirect the user to the home page using <Redirect/> from react-router after they have successfully logged in. However, I'm facing an issue where the timeout is functioning corr ...

Can the characteristics of a group of objects be combined and sorted based on a different attribute?

Feeling a bit puzzled by my title, Here's the issue at hand - I aim to destructure an array of objects to utilize its properties in a chartJS Line Graph. Take a look at the replicated array below: [{ "page": "Page 1", &qu ...