The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> focus on input field
John Kale
post Feb 19 2009, 11:17 PM
Post #1





Group: Members
Posts: 1
Joined: 19-February 09
Member No.: 7,850



Can anybody tell me what is i am doing wrong with the following code. all i want is if the name field is bland then it should give an alert and then focus on the input field, but what its doing is it gives the alert and then focus and then refresh the page.

CODE
<html>
<head>
<script>

function checkform(){

if (document.form1.name1.value.length<1)
{
window.alert("enter your name")
document.form1.name1.focus()}
else{
window.open("http://www.msn.com")}
}
</script>
</head>
<body>
<form name="form1">
<input type="text" name="name1" size="35">
<input type="submit" name="submit" value="Submit" OnClick="checkform()">

</form>
</body>
</html>

User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Brian Chandler
post Feb 20 2009, 01:40 AM
Post #2


Jocular coder
********

Group: Members
Posts: 2,460
Joined: 31-August 06
Member No.: 43



You need to set the result of your checkform() function to TRUE or FALSE (sorry can't remember which) to prevent the submit from taking effect anyway.

User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
geoffmerritt
post Mar 7 2009, 03:50 AM
Post #3


Member
***

Group: Members
Posts: 66
Joined: 23-December 08
From: Adelaide
Member No.: 7,394



Coding..... don't you hate it when you forget some of these ;

CODE

if (document.form1.name1.value.length<1)
  {
  window.alert("enter your name")
  document.form1.name1.focus()
  }
else
  {
  window.open("http://www.msn.com")}
  }


Try this code

CODE

if (document.form1.name1.value.length<1)
  {
  window.alert("enter your name");
  document.form1.name1.focus();
  return false;
  }
else
  {
  window.open("http://www.msn.com");
  }

User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 18th April 2024 - 07:27 PM