Error Encountered During JavaScript Form Validation

Currently, I am troubleshooting a website that was created by another developer. There is a form with JavaScript validation to ensure data is accurately entered into the database. However, I am puzzled as to why I keep receiving these alert messages. Please refer to the code snippet below:

function save(){
  if (echeck(document.getElementById('email').value)==false){
    document.getElementById('email').focus();
  }else if(document.getElementById('fullname').value==''){
    alert("Full name is required");
    document.getElementById('fullname').focus();
  }else if(document.getElementById('handphone').value==''){
    alert("HP number is required");
    document.getElementById('handphone').focus();
  }else if(document.getElementById('bank_name').value==''){
    alert("Bank Account Name is required");
    document.getElementById('bank_name').focus();
  }else if(document.getElementById('bank_number').value==''){
    alert("Bank Account Number is required");
    document.getElementById('bank_number').focus();
  }else if(document.getElementById('code').value==''){
    alert("Captcha Code cannot be blank");
    document.getElementById('code').focus();
  }else{
    $.post("<?php echo base_url(); ?>index.php/misterjudi/register/", {
      email:document.getElementById('email').value,
      fullname:document.getElementById('fullname').value,
      handphone:document.getElementById('handphone').value,
      bank:document.getElementById('bank').value,
      bank_name:document.getElementById('bank_name').value,
      bank_number:document.getElementById('bank_number').value,
    },
    function(data){
      alert("Successfully registered");
      location.href="<?php echo base_url(); ?>";
    });
  }
}

It seems like the script checks for empty fields before submitting the form. Even when all the fields are filled out correctly, I still encounter alerts such as ('Full name is required').

I appreciate any assistance you can provide.

Additionally, here is the HTML code for reference:

<table style="border:0px solid #CCC; width:100%;">
  <tr>
    <td style="padding-bottom:10px; width:180px;">Email
      <span style="color:#F00;">*</span></td><td style="padding-left:20px; padding-bottom:10px;">
      <input id="email" type="text" name="email" onblur="check_email(this.value);" />
    </td>
  </tr>
  <tr>
    <td style="padding-bottom:10px;">Nama <span style="color:#F00;">*</span>
    </td>
    <td style="padding-left:20px; padding-bottom:10px;">
      <input id="fullname" type="text" name="fullname" />
    </td>
  </tr>
  <tr>
    <td style="padding-bottom:10px;">No HP <span style="color:#F00;">*</span>
    </td>
    <td style="padding-left:20px; padding-bottom:10px;">
      <input id="handphone" type="text" name="handphone" />
    </td>
  </tr>
  <tr>
    <td style="padding-bottom:10px;">Nama Bank <span style="color:#F00;">*</span></td>
    <td style="padding-left:20px; padding-bottom:10px;">
      <select name="bank" id="bank">
        <?php $data=$this->dubol_model->get_bank(); ?>
        <?php for($i=0;$i<count($data);$i++){ ?>
        <option value="<?php echo $data[$i]['BankCode']; ?>">
          <?php echo $data[$i]['BankName']; ?></option>
          <?php } ?>
      </select>
    </td>
  </tr>
  <tr>
    <td style="padding-bottom:10px;">Nama Rekening <span style="color:#F00;">*</span></td>
    <td style="padding-left:20px; padding-bottom:10px;"><input id="bank_name" type="text" name="bank_account_name" /></td>
  </tr>
  <tr>
    <td style="padding-bottom:10px;">Nomor Rekening <span style="color:#F00;">*</span></td>
    <td style="padding-left:20px; padding-bottom:10px;">
      <input id="bank_number" type="text" name="bank_account_number" />
    </td>
  </tr>
  <tr>
    <td colspan="2">
      <div style="width: 430px; padding-top:20px; padding-bottom:20px; float: left; height:90px; background-color:#FFF; border:1px solid #CCC;">
        <img id="siimage" align="left" style="padding-right: 5px; border: 0" src="<?php echo base_url(); ?>/captcha/securimage_show.php?sid=<?php echo md5(time()) ?>" />
        <script type="text/javascript">
          AC_FL_RunContent( 'codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0','width','19','height','19','id','SecurImage_as3','align','middle','src','<?php echo base_url(); ?>/captcha/securimage_play?audio=<?php echo base_url(); ?>/captcha/securimage_play.php&bgColor1=#777&bgColor2=#fff&iconColor=#000&roundedCorner=5','quality','high','bgcolor','#ffffff','name','SecurImage_as3','allowscriptaccess','sameDomain','allowfullscreen','false','pluginspage','http://www.macromedia.com/go/getflashplayer','movie','<?php echo base_url(); ?>/captcha/securimage_play?audio=<?php echo base_url(); ?>/captcha/securimage_play.php&bgColor1=#777&bgColor2=#fff&iconColor=#000&roundedCorner=5' ); //end AC code
        </script>
        <noscript>
          ...
        </noscript><br />
        ...
      </div>
    </td>
  </tr>
  <tr>
    <td style="padding-top:10px;">Code <span style="color:#F00;">*</span></td>
    <td style="padding-top:10px;">
      <input id="code" type="text" name="code" size="12" />
    </td>
  </tr>
  <tr>
    <td colspan="2" style="padding-top:20px;">
      <input type="button" value="    Close    " onclick="$('#register').animate({  height: 'toggle', opacity: 'toggle'}, 1000);" />
      <input type="button" value="Register" onclick="save();" />
    </td>
  </tr>
</table>

Answer №1

For troubleshooting purposes, add the following line of code before 'alert("Fullname is required");': console.log(document.getElementById('fullname').value, document.getElementById('fullname').value === '');

By doing this, the web inspector console will display the input value and indicate whether it is an empty string. This should provide insight into the problem at hand.

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

Issue encountered while trying to download Jade through npm (npm install -g jade)

I am having trouble downloading jade via npm on my Mac (Yosemite). After downloading node and updating npm, I tried to install jade but encountered a series of errors that I cannot resolve. Even attempting to use sudo did not help, as it only displayed s ...

What is the best MySQL data type for storing JavaScript code with PHP?

I am creating a platform that resembles jsfiddle, allowing users to store their JavaScript codes and retrieve them in an organized manner. I am unsure about which data type would be most suitable for saving the codes, or if storing them in text files wou ...

Run code once the Firestore get method has completed its execution

Is it possible to execute code after the get method is finished in JavaScript, similar to how it can be done in Java (Android)? Below is an example of my code: mColRef.get().then(function(querySnapshot){ querySnapshot.forEach(function(doc) { ...

Unable to utilize JavaScript from the imported AJAX page XMLHttpRequest

I have implemented a bit of AJAX functionality using XMLhttpRequest, where it replaces a div and functions as expected. However, I am facing difficulty in getting the JavaScript code in the included AJAX page to execute. Is there a way to run JavaScript f ...

"Threads snap as soon as they begin to be put to use

Encountering an issue with the command yarn run serve, the error log states: $ vue-cli-service serve INFO Starting development server... 10% building 2/2 modules 0 activeevents.js:173 throw er; // Unhandled 'err ...

ng-bind stops updating after entering text into input field

I am a newcomer to AngularJS and I have encountered an issue that I am struggling to resolve. Although I found a similar question on stackoverflow, the solution provided did not work for me. The problem I am facing is that when I input text into any of the ...

What can be done to improve the elegance of this jQuery function?

I am facing an issue on my webpage where I have a drop-down select box with a default value of (empty). The problem arises when the user selects an entry, and the appropriate form for that entry type should be displayed. However, there are a couple of iss ...

Preventing the insertion of a line break when using Shift + Enter in Vuejs

Whenever I use a textarea to enter text, I find that I have to press Shift + Enter every time to send the text. However, upon sending, it adds /n at the end. I prefer using the Enter key for newline instead of submitting the form. Example: hello => ...

Error encountered in Webpack configuration while using html-webpack-plugin to generate index.html file

I've been experimenting with webpack to bundle project JS files. My goal is to generate an index.html file under the output dist folder using webpack. To achieve this, I followed the instructions provided in the webpack documentation and installed "h ...

Tips for toggling a menu by clicking a link

I have a Navbar component with a hamburger menu. When the hamburger menu is clicked, I want to display the menu component, which is separate. To achieve this, I passed data through props and made it work. Now, I want the menu to close when clicking outsi ...

Triggering AJAX call from several buttons within a single page in Django

Hey there! I'm currently working on implementing a voting feature using Ajax in my Django-based website. The issue I'm facing is that users can only vote on the first entry, but I want them to be able to vote on all entries. Can you assist me wit ...

Achieving uniform width in material ui: Tips for maintaining consistency

I am encountering an issue with the width of my Goal components and can't figure out what is causing it. https://i.stack.imgur.com/jPlyf.png Despite setting the width of the Paper selector to 100%, each Goal component's width remains inconsiste ...

transferring data from one HTML file to another using a loop with technologies such as HTML,

As a beginner in front end development, I am finding it a bit challenging at the moment. Here's what I have so far: 1 main HTML file (index.html) 1 JavaScript file (something.js) 2nd HTML file (something.html) Main HTML: <!DOCTYPE html> < ...

Mastering the usage of AngularJS Directive controllerAs syntax with scope is key to developing

I have included my code below: // HTML <body> <h1>{{foo.name}}</h1> <my-directive></my-directive> </body> // Scripts app.directive('myDirective', function() { return { restrict: 'E', ...

Avoiding data type conversion in JavaScript/TypeScript

Currently delving into the world of JavaScript, I come from a background of working with statically typed languages. Naturally, I opted to dive into TypeScript instead of starting directly with JS. While TypeScript is great and addresses many issues presen ...

Ensure the validation of numerous input fields within a single form with the use of Vue 3 composition API

Within my application, I have implemented custom validation and validators to ensure the accuracy of various form fields. The submit button functionality is designed to remain disabled until all input fields are filled. The creation of validators involves ...

What is the best way to set the keyframes CSS value for an element to 0%?

Would like to create a progress indicator div using CSS animations? Check out the JSBin link provided. The desired behavior is to have the div width increase from 30% to 100% when the user clicks on stage3 after stage1, rather than starting from 0%. Althou ...

How to eliminate a particular item within a string only in rows that include a different specific item using JavaScript

Is my question clear? Here's an example to illustrate: let string = "Test text ab/c test Test t/est ### Test/ Test t/test Test test" I want to remove / only from the line that includes ###, like so: Test text ab/c test Test t/est ### Test T ...

What is the process for obtaining a JSON result after completing an upload?

I recently started working with the razor view engine and I have encountered a situation where I need to upload an image using the Change event of an image browser. The jQuery function for this task is as follows: $("#billUpload").change(function (){ ...

Include the await keyword within the .then block

I'm trying to execute an await after receiving a response in the .then callback of my code: const info = new getInfo(this.fetchDetails); info .retrieve() .then((res) => { const details = this.getLatestInfo(res, 'John'); }) .ca ...