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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:06:08+00:00 2026-05-18T11:06:08+00:00

Correct me if I am wrong, but isn’t it possible to declare a variable

  • 0

Correct me if I am wrong, but isn’t it possible to declare a variable right after the script tag, so that it can be used throughout that script? I tried this, and my functions are acting as if it isn’t even there. Did I do something wrong, or is this supposed to happen. I would hate to have to redeclare all of my variables for each function if they are the exact same thing.

Sorry about that

<script>
  var na=document.getElementById('nr');
  var ea=document.getElementById('er');
  var em=document.subscribe.email;
  var fn=document.subscribe.fname;
  var ln=document.subscribe.lname;
  var eml=document.subscribe.email.value.length;
  var fnl=document.subscribe.fname.value.length;
  var lnl=document.subscribe.lname.value.length;
  var at=document.subscribe.email.value.indexOf("@");
  var per=document.subscribe.email.value.lastIndexOf("."); 

function validate_form() {
  if((fnl<1 || lnl<1) && !eml<1){
      alert("Please enter your first and last name.")
      if(fnl<1){fn.focus()}else{ln.focus()}
      }
  else if((fnl<1 || lnl<1) && eml<1){
      alert("Please fill in all fields.")
      if(fnl<1){fn.focus()}else{ln.focus()}
      }  
  else if(eml<1 || at<1 || per-at<2 || eml-per<2){
      alert("Please enter a valid email address")
      em.focus()
      }    
  else if (at>1 && per-at>2 && eml-per>2 && fnl>1 && lnl>1){return true}
  vfn(); vln(); vem();
 return false}

It works perfectly fine when the vars are INSIDE the function, but when they are like this, nothing at all happens.

  • 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-18T11:06:09+00:00Added an answer on May 18, 2026 at 11:06 am

    I actually figured out how to do it. I just read about the global variables, and how to declare them inside a function. So I put all the vars back inside the function, erased the “var” on it and it works perfect now.

    function validate_form() {
       na=document.getElementById('nr');
       ea=document.getElementById('er');
       em=document.subscribe.email;
       fn=document.subscribe.fname;
       ln=document.subscribe.lname;
       eml=document.subscribe.email.value.length;
       fnl=document.subscribe.fname.value.length;
       lnl=document.subscribe.lname.value.length;
       at=document.subscribe.email.value.indexOf("@");
       per=document.subscribe.email.value.lastIndexOf("."); 
      if((fnl<1 || lnl<1) && !eml<1){
          alert("Please enter your first and last name.")
          if(fnl<1){fn.focus()}else{ln.focus()}
          }
      else if((fnl<1 || lnl<1) && eml<1){
          alert("Please fill in all fields.")
          if(fnl<1){fn.focus()}else{ln.focus()}
          }  
      else if(eml<1 || at<1 || per-at<2 || eml-per<2){
          alert("Please enter a valid email address")
          em.focus()
          }    
      else if (at>1 && per-at>2 && eml-per>2 && fnl>1 && lnl>1){return true}
      vfn(); vln(); vem();
     return false}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to trim JSP whitespace using the trimSpaces directive in my web.xml, but
I am pretty confused in collection and IEnumerable types. Can any one explain why
I have a View and View Model. The View Model has a bool on
I'm afraid I have a question on a detail of a rather oversaturated topic,
I have played for a while with OpenGL on Android on various devices. And
I'm trying to make my contact form submission process work equally well whether the
I'm writing a new tab extension for Firefox, and I'd like to have a
I am using the following function to insert a new node into a sorted
I'm creating textboxes in the Page_Load event and giving them values, however whenever I
This is I guess a question in a few parts. I'm working through the

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.