Hey
I am new to coding and is stuck as I am trying to use javascript function to make an onblur effekt. I want to make a box with "First Name" (which is working) and then allow the user to type in between 2-15 characters. If less it used, the site should say "Enter between 2 and 15 characters" with unblur effect and if it is between 2-15 characters it should say "valid"....
I have made my html and my javascript file/code in two separate files and linked them through
<script type="text/javascript" src="formvalidation1.js"></script>
(I am not sure, how to post correctly in these forums, so I try. Please do correct me if I can post the code in a way, that makes it easier for you to read (pictures of the code attached)
(NOT CODE. STARTS HERE)
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title> Form Validation</title>
<script type="text/javascript" src="formvalidation1.js"></script>
</head>
<body>
<form action="#">
<div id="fullpage">
<H1> Make an order</H1>
<table border="0">
<tr>
<td class="label_col">First Name:</td>
<td class="input_col"><input name="firstName" id="firstNamePrompt" type="text" onblur="validateFirstName ();"></td>
<td class="feedback_col"><span id="firstNamePrompt"> </span>
</tr>
</body>
</html>
(NOT CODE. FORM VALIDATION1.JS FILE below)
function validateFirstName () { //test unput for 2-15 charachters allowed
var fName =document.getElementById("firstName").value;
var rel=/^[a-zA-Z\s\'\-] {2,15}$/;
if (rel.test(fName)) { // if input i sbalid, update page to show succes
document.getElementById("firstNamePrompt").style.color="green";
document.getElementById("firstNamePrompt").innerHTML="<strong>valid</strong>"
return true;
}
else { //if input is invalid, update page to promt for new input
document.getElementById("firstNamePrompt").style.color="red";
document.getElementById("firstNamePrompt").innerHTML= "<strong>Enter between 2 and 15 characters</strong>"
return false;
}
}
I am new to coding and is stuck as I am trying to use javascript function to make an onblur effekt. I want to make a box with "First Name" (which is working) and then allow the user to type in between 2-15 characters. If less it used, the site should say "Enter between 2 and 15 characters" with unblur effect and if it is between 2-15 characters it should say "valid"....
I have made my html and my javascript file/code in two separate files and linked them through
<script type="text/javascript" src="formvalidation1.js"></script>
(I am not sure, how to post correctly in these forums, so I try. Please do correct me if I can post the code in a way, that makes it easier for you to read (pictures of the code attached)
(NOT CODE. STARTS HERE)
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title> Form Validation</title>
<script type="text/javascript" src="formvalidation1.js"></script>
</head>
<body>
<form action="#">
<div id="fullpage">
<H1> Make an order</H1>
<table border="0">
<tr>
<td class="label_col">First Name:</td>
<td class="input_col"><input name="firstName" id="firstNamePrompt" type="text" onblur="validateFirstName ();"></td>
<td class="feedback_col"><span id="firstNamePrompt"> </span>
</tr>
</body>
</html>
(NOT CODE. FORM VALIDATION1.JS FILE below)
function validateFirstName () { //test unput for 2-15 charachters allowed
var fName =document.getElementById("firstName").value;
var rel=/^[a-zA-Z\s\'\-] {2,15}$/;
if (rel.test(fName)) { // if input i sbalid, update page to show succes
document.getElementById("firstNamePrompt").style.color="green";
document.getElementById("firstNamePrompt").innerHTML="<strong>valid</strong>"
return true;
}
else { //if input is invalid, update page to promt for new input
document.getElementById("firstNamePrompt").style.color="red";
document.getElementById("firstNamePrompt").innerHTML= "<strong>Enter between 2 and 15 characters</strong>"
return false;
}
}