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

Ajax call encounters 404 error but successfully executes upon manual page refresh (F5)

I am encountering a frustrating issue with my javascript portal-like application (built on JPolite) where modules are loaded using the $.ajax jquery call. However, the initial request fails with a 404 error when a user first opens their browser. The app i ...

When trying to retrieve an XML file that contains an escaped ampersand, a jQuery AJAX call is throwing an error

Before sending text input to a server to be stored in a MySQL database using XML, I first escape the "&" characters: var copyright = copyright.replace(/&/g,"&amp;"); The wrapped XML data block is then sent to the server through jQuery's ...

Utilizing Shopify API to seamlessly add items to cart without any redirection for a smoother shopping experience

Looking for a solution to submit an add to cart POST request without any redirection at all? I have tried changing return_to to "back" but that still reloads the page, which is not ideal. My goal is to smoothly add the item to the cart and notify the cli ...

Utilizing external JavaScript libraries in Typescript for integration with nodeJS

We've recently made the switch to using Typescript + Electron for developing a browser-based desktop application. However, we often encounter delays when loading external Javascript libraries. While typings helps with most of our needs, there are stil ...

Using Node.js to retrieve a p12 certificate from the certificate repository

Is there a way to retrieve the p12 certificate from the certificate store after it has been installed? I am facing a situation where both the private key and certificate are combined in a p12 certificate and then stored in the Windows certificate store. ...

Tips for overcoming a challenge with a promise of $q

Currently in my AngularJS project, I am utilizing $q for promises and feeling a bit overwhelmed with the usage. How can I resolve this promise-related issue? My goal is to either return the user when isLoggedInPromise() is triggered or provide a response ...

Make sure to validate onsubmit and submit the form using ajax - it's crucial

Seeking assistance for validating a form and sending it with AJAX. Validation without the use of ''onsubmit="return validateForm(this);"'' is not functioning properly. However, when the form is correct, it still sends the form (page r ...

JQuery is having trouble with playing multiple sound files or causing delays with events

I've been working on a project that involves playing sounds for each letter in a list of text. However, I'm encountering an issue where only the last sound file is played instead of looping through every element. I've attempted to delay the ...

Common issues encountered when using the app.get() function in Node.js

I've been attempting to develop a website that utilizes mongojs. I've implemented the code snippet below, but when I launch the site, it never reaches the app.get() section, resulting in a 500 error on the site. How can I ensure it responds to th ...

Retrieve a unified data array from the provided data source

Below is a snapshot of my data const data = [{"amount": "600,000", "cover": null, "id": "1", "img": "636e56de36301.1.png", "make": "bmw", "model": "bmw ...

Creating Table Rows on-the-fly

Seeking assistance and guidance from the community, I have modified code from an online tutorial to allow users to dynamically add rows to a table when data is entered in the row above. However, I am facing an issue where I can only insert one additional ...

Enter your text in the box to search for relevant results

I need assistance with a code that allows me to copy input from a text box to the Google search box using a copy button. Upon pressing the Google search box button, it should display the search results. Here is the code I have been working on: http://jsf ...

Stop form from submitting on bootstrap input, still check for valid input

Encountering a dilemma here: Employing bootstrap form for user input and utilizing jQuery's preventDefault() to halt the form submission process (relying on AJAX instead). Yet, this approach hinders the input validation functionality provided by boots ...

JS Nav Dots are not activating the Active Class

I have been utilizing a code snippet from this source to incorporate a vertical dot navigation feature into a single-page website. The navigation smoothly scrolls to different sections when a link is clicked, with an active highlight on the current section ...

Error occurs in ReactJS App when the state is updated in Redux only after dispatching the same action twice

Check out the following code snippet: ChartActions.js import * as types from './ChartTypes.js'; export function chartData(check){ return { type: types.CHART_DATA,check }; } ChartTypes.js export const CHART_DATA = 'CHART_DATA ...

What steps can be taken to resolve an ESLing error in this situation?

Check out this code snippet: <template v-if="isTag(field, '')"> {{ getItemValue(item, field) ? getItemValue(item, field) : '&#8211'; }} </template> An issue has been identified with the above code: Er ...

Is there a way to utilize req.query, req.params, or req.* beyond its original scope without the need to store it in a database?

Looking to streamline my code and apply the DRY pattern, I've been working on creating a helper function for my express http methods. The structure of each method is similar, but the req.params format varies between them. Here's how I attempted t ...

What is the method to permanently install and enforce the latest version using npm?

We are implementing a private npm module for exclusive use within our organization. Given that the module is managed internally, we have confidence in version updates and changes. Is there a way to seamlessly install this module across multiple projects s ...

Incorrect Request Method for WCF json POST Request Leads to 405 Error (Method Not Allowed)

Hey there, I'm facing an issue with using Microsoft Visual Studio to create a WCF web service. Everything runs smoothly within Visual Studio, but when I try to connect to the service from outside, it fails to establish a connection. At first, I encoun ...

Using AngularJS to pass radio button value to a $http.post request

Can you please advise on how to extract the value from a radio button and send it using $http.post in AngularJS? Here is an example: HTML <input name="group1" type="radio" id="test1" value="4"/> <label for="test1">Four paintings</label ...