简体   繁体   中英

How to include an external Javascript to html5

Problem: External javascript is not working.
I want my registration page to be validated and I have written the code but I don't know where to call it I tried the following.

This is my javascript code:

var emailRegex = /^[A-Za-z0-9._]*\@[A-Za-z]*\.[A-Za-z]{2,5}$/;
var fname = document.form.Name.Value;
var lname = document.form.Last.value;
var fpassword = document.form.password.value;
var frassword = document.form.repassword.value;
var femail = document.form.Email.value;

function submit()
{
   if ( fname === "")
   {
       document.form.Name.focus();
       alert("Please enter the name");
       return false;
   }
   if ( lname === "")
   {
       document.form.Last.focus();
       alert("Please enter last name");
       return false;
   }
   if( fpassword === "")
   {
       document.form.password.focus();
       alert("You can't leave password empty");
       return false;
   }
   if( frassword === "")
   {
       document.form.repassword.focus();
       alert("Please confirm password");
       return false;
   }
   if( femail === "")
   {
       document.form.Email.focus();
       alert("Don't leave Email blank");
       return false;
   }
   else if (!emailRegex.test(femail))
   {
       document.form.Email.focus();
       alert("Not a valid Email");
       return false;
   }
   if( fname !== '' && lname !== '' && fpassword !== '' && frassword !== ''       && femail !== '')
   {
       alert("Registration sucessfull");
   }
}

I tried like this :

<script type="text/javascript" src="skill.js"></script>
<script>
  submit();
</script>

And I tried this too:

<form name="form" onclick="submit()">

create a submit button inside the form element like this -

<input type="submit" value="Submit" />

and then add an attribute on the opening form tag, like this-

<form name="form" onsubmit="submit()">

make sure you have linked to you JS file already.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM