Search code examples
javascriptfunctionfunction-calls

Trying to call a function inside JavaScript file has no action


I recreated my mobile application I am trying to develop using CLI this time, so that I can add my plugins in my Phonegap 3.4 with a much easiest way.

The problem is that in my JavaScript, when I call the registerdb function, nothing works fine. I mean even though I do mistake in the password or in the email text areas when I run it, I don't get any vibrate or error message as I should.

When I put in comments the calling of the registerdb and the function, the JavaScript works correct until it's end (it prints the alerts when every text input is correct).

So, I believe that the problem is related to the calling of function registerdb. Any help ?


My .js file:

document.addEventListener("deviceready", onDeviceReady, false);

    // PhoneGap is ready
    function onDeviceReady() {
        // Empty
    }


// Function to add event listener to register button
function load() { 
  var el = document.getElementById("register"); 
  el.addEventListener("click", Register, false); 
} 

document.addEventListener("DOMContentLoaded", load, false);

function Register() {
  var username = document.getElementsByName('username')[0];
  var password = document.getElementsByName('password')[0];
  var email = document.getElementsByName('email')[0];

  if (username.value == "") {
    $("#username").focus();
    document.getElementById('username').style.boxShadow = "0 0 7px #f00";
    navigator.notification.vibrate(500);
  } 
  else{
    document.getElementById('username').style.boxShadow = "none";
  }

   //>5 characters, 1 upper case, at least 1 lower case, at least 1 numerical character, at least 1 special character
   var passExp = /(?=^.{6,15}$)((?=.*\d)(?=.*[A-Z])(?=.*[a-z])|(?=.*\d)(?=.*[^A-Za-z0-9])(?=.*[a-z])|(?=.*[^A-Za-z0-9])(?=.*[A-Z])(?=.*[a-z])|(?=.*\d)(?=.*[A-Z])(?=.*[^A-Za-z0-9]))^.*/;
   var strong_flag_pass = 0;

  if (!(password.value.match(passExp))) {
    $("#password").focus();
    document.getElementById('password').style.boxShadow = "0 0 7px #f00";
    navigator.notification.alert("Please enter a strong Password! It has to have at least: 6 characters, 1 upper case, 1 lower case, 1 numerical character and 1 special character!", null, 'Password', 'Okay');
    navigator.notification.vibrate(500);
    strong_flag_pass = 0;
  }
  else{
    document.getElementById('password').style.boxShadow = "none";
    strong_flag_pass = 1;
  }

  var emailExp = /^.+@[^\.].*\.[a-z]{2,}$/;
  var strong_flag_email = 0;

  if (!(email.value.match(emailExp))) {
    $("#email").focus();
    document.getElementById('email').style.boxShadow = "0 0 7px #f00";
    navigator.notification.alert("Please enter a correct Email!", null, 'Email', 'Okay');
    navigator.notification.vibrate(500);
    strong_flag_email = 0;
  }
  else {
    document.getElementById('email').style.boxShadow = "none";
    strong_flag_email = 1;
  }

  var gender = document.getElementById("gender").value;

  var about_me = document.getElementById("about-me").value;

  var age = document.getElementById("radio-choice").value;

  if (document.getElementById('radio-choice-1').checked) {
    age = document.getElementById('radio-choice-1').value;
  }
  else if (document.getElementById('radio-choice-2').checked) {
    age = document.getElementById('radio-choice-2').value;
  }
  else if (document.getElementById('radio-choice-3').checked) {
    age = document.getElementById('radio-choice-3').value;
  }
  else if (document.getElementById('radio-choice-4').checked) {
    age = document.getElementById('radio-choice-4').value;
  }
  else if (document.getElementById('radio-choice-5').checked) {
    age = document.getElementById('radio-choice-5').value;
  }
  else if (document.getElementById('radio-choice-6').checked) {
    age = document.getElementById('radio-choice-6').value;
  }

  if (username.value != "" && strong_flag_pass == 1 && strong_flag_email == 1){      
    alert(gender);
    alert(about_me);
    alert(age);

    //add to db
    registerdb(email, password, username, gender, about_me, age);
  }


 function registerdb(email, password, username, gender, about_me, age) {
   alert(gender);
   alert(about_me);
   alert(password;
 }
}

In my .html the way I "call" this function is :

                            <a href="#" id="register" data-role="button" data-theme="a">Register</a>     

Solution

  • You are missing a ) after alert(password;

    function registerdb(email, password, username, gender, about_me, age) {
        alert(gender);
        alert(about_me);
        alert(password);
    }