function firstname()
{
var x=document.forms["frm"]["fname"].value;
if(x=="")
{
document.getElementById("checkfname").value = " First Name required";
return false;
}
else
{
document.getElementById("checkfname").value = "";
return true;
}
}
<html>
<body>
<form method="post" name="frm">
<input type="text" name="fname" onkeypress="firstname()" onkeydown="firstname()" onkeyup="firstname()"/>
<input name="checkfname"/>
</body>
</html>
cant get the function run at all in chrome or Firefox?? it isn't firing up. Any help pls?
First of all make sure you have javascript code inside the script tag and then
you are using document.getElementById("checkfname") but you haven't give checkfname as Id of any element. So it's gonna be undefined element so change it like this
<input id="checkfname"/>
and then try your code.
JsFiddle
Related
I am making a site on google sites.
I have a button made by code that detects if you put a certain value into an input box, it should then load another site. But when I enter that correct value, the object crashes.
I've tried a lot of different things, but nothing works.
<form name="form1" method="GET">
<input name="codebox1" type="text" />
<input name="button1" type="submit" value="Check Code" onClick="return testResults(this.form)"/>
</form>
<script>
function testResults (form) {
if (form.codebox1.value == "Doovhhlqjhbh") {
window.location = 'https://sites.google.com/view/the-mbd-project/There-are-secrets-to-be-discovered';
window.alert("Correct Code. Connecting...");
}
else {
window.alert("Wrong Code, try again!");
}
return false;
};
</script>
window.location = string; is wrong . You should use this to change the browser address. Also, alert must be executed before switching the page
change your function to this:
function testResults(form) {
if (form.codebox1.value == "Doovhhlqjhbh") {
window.alert("Correct Code. Connecting...");
setTimeout(() => {
window.location.href = 'https://sites.google.com/view/the-mbd-project/There-are-secrets-to-be-discovered';
}, 0);
}
else { window.alert("Wrong Code, try again!"); }
return false;
}
How can I prevent submision if in my text field are entered just specific characters <>{} and not all of special characters? I'm losing my mind :/
I think you are looking for regex expression. Let me know if it's helpful
$(":input").each(function() {
var input = $(this).val();
var regex = new RegExp("^[a-zA-Z]+$");
if(regex.test(input)) {
alert("true");
} else {
alert("false");
return false;
}
})
I don't have an exact answer as you didn't post any sample code. I can only point you to this article https://javascript.info/bubbling-and-capturing which explain how events bubbling works. A solution will be to use event.stopPropagation() in case the validation doesn't pass.
Here an working ex:
<!DOCTYPE html>
<html>
<head>
<script>
function validateForm() {
const regex = new RegExp('^[a-zA-Z]+$');
const input = document.forms['someForm']['somename'].value;
if (regex.test(input)) {
console.log("true");
} else {
console.log("false");
return false;
}
}
</script>
</head>
<body>
<form name="someForm" onsubmit="return validateForm()" method="post">
Name: <input type="text" name="somename">
<input type="submit" value="Submit">
</form>
</body>
</html>
I am using this code..
Error is showing but focus is not working in firefox. As this code is working in IE, i can't say this code is completely wrong.
<form name="frm" action="search.php" method="post">
<input type="text" name="search" onblur="return check();"/>
<input type="submit" name="submit" />
<strong id="err"></strong>
</form>
I am using this string in external javascript.
This code is in valid.js
function check()
{
var item=frm.search;
var errr=document.getElementById('err');
if(item.value.length<3)
{
item.focus();
errr.innerHTML="Entered String is very short";
return false;
}
}
Please reply me as soon as possible.
try this one
function check()
{
var item = document.forms['frm'].elements['search'];
var errr=document.getElementById('err');
if(item.value.length<3)
{
errr.innerHTML="Entered String is very short";
setTimeout(function() {
item.focus()
}, 10);
return false;
}
}
demo jsfiddle http://jsfiddle.net/ff4vW/
Its good if you use document.getElementById()
But if you are using name
Then you should use
var item = document.forms['frm'].elements['search'];
var item = document.getElementsByName('search')[0];
I'm writing a simple function just for fun for a friend, but my js function wont return x the way I want it.
I have a text field and a button. If a user inputs "no good lyer" x should return "Big Fucking Surprise!"
If a user inputs anything else with text, x should return "Leave her ass anyway!"
If a user doesn't input anything x should return "Make a decision!"
Here is my code:
<!DOCTYPE html>
<html>
<body>
<p>Click the button to make a decision about your ex.</p>
<p id="choice">Here is your Decision</p>
<form>
My Ex:<input type="text" name="myEx">
</form>
<button onclick="myLife()">Decide</button>
<script>
function myLife()
{
var x="";
if("myEx".text=="no good lyer")
{
x="Big Fu**ing Surprise!";
}
if("myEx".text!="no good lyer")
{
x="Leave her a** anyway!";
}
if("myEx".text=="")
{
x="Make a decision!"
}
document.getElementById("choice").innerHTML=x
}
</script>
</body>
</html>
Sorry but javascript strings don't have methods called text. your trying to get the value of the input <input type="text" name="myEx"> with 'myEx'.text, which is strange to say the least but you need to use normal people javascript. try getElementsByName('myEx')[0]
Add this variable to the top of your function myLife:
var myEx = document.getElementsByName('myEx')[0].value
change every line like this:
"myEx".text
to simply this
myEx
First of all you should use === and !== operator to check for conditions, Try the code below
To get a value from a input text use .value
<!DOCTYPE html>
<html>
<body>
<p>Click the button to make a decision about your ex.</p>
<p id="choice">Here is your Decision</p>
<form>
My Ex:<input type="text" id="myText" value="">
</form>
<button onclick="myLife()">Decide</button>
<script>
function myLife()
{
var myText = document.getElementById("myText");
var x="";
if(myText.value==="no good lyer")
{
x="Big surprise!";
}
if(myText.value!=="no good lyer")
{
x="Leave her!";
}
if(myText.value === "")
{
x="Make a decision!"
}
document.getElementById("choice").innerHTML=x
}
</script>
</body>
</html>
change your script to following
<script>
function myLife()
{
var x="";
if("myEx".text=="no good lyer")
{
x="Big Fu**ing Surprise!";
}
if("myEx".text!="no good lyer")
{
x="Leave her a** anyway!";
}
if("myEx".text==null)
{
x="Make a decision!"
}
document.getElementById("choice").innerHTML=x
}
</script>
My following JavaScript is not working:
<script type="text/javascript">
function checkDetails(search)
{
var search = documment.getElementById('query');
if(search.value ==''||search.value==null)
{
alert('No search criteria entered');
query.focus;
return false;
}
}
</script>
</head>
<body>
<form name="search" action ="123.php" onSubmit="return checkDetails(this);" method ="get">
<p><input type ="text" id = "query" name ="query" />
<input type ="submit" value ="Web Service"/></p>
</form>
</body>
Try this code:
function checkDetails(search)
{
var search = document.getElementById('query');
if(search.value ===''||search.value===null)
{
alert('No search criteria entered');
search.focus();
return false;
}
return true;
}
You wrote documment.
You don't specify in what way it's not working - you get an error, it doesn't validate correctly, you don't get the error alert? One thing I spotted:
query.focus;
Should be
search.focus();
Also you misspelled document as documment
Also try something like this
onSubmit="return checkDetails(this,this.id);"
function checkDetails(search,id)
{
var search = documment.getElementById(id);
if(search.value ==''||search.value==null)
{
alert('No search criteria entered');
query.focus;
return false;
}
}
It will be
search.focus();
instead of
query.focus;
and documment will be document