/    Sign up×
Community /Pin to ProfileBookmark

How to validate form and post result on same page

Hi, I want to validate a form using javascript. On submit, I want it to check each text box, and if there was an error, instead of deleting the values in the text box, I want it to give a message on the top of the page and not have any of the values deleted.

I am currently only working on the first name text box, once I get that down, I reckon I can expand it and work on the rest using the same method.

[CODE]
<html>
<head>
<title>Form</title>
<style type=”text/css”>
input {margin:5px}
div
{
width:150px;
float:left;
overflow:hidden;
}
</style>
<script type=”text/javascript”>
<!–
function validate( form )
{
var regexpstr=/^[a-zA-Z]+$/;

var fname=form.first_name.value;

if(fname==” “||fname==””){
alert(“Please enter your first name”);
return false;
}
else if(fname.match(regexpstr)){
alert(“OK”);
return true;
}
else{
alert(“Please check if you have entered your first name correctly”);
return false;
}
}
–>
</script>
</head>

<body>
<form onsubmit=”return validate(this)”>
<div>First Name: </div>
<input type=”text” name=”first_name” size=”11″ /> <br />
<!–
<div>Surname: </div>
<input type=”text” name=”surname” size=”11″ /> <br />
<div>Address Line 1:</div>
<input type=”text” name=”address_line_1″ size=”20″ /> <br />
<div>Address Line 2: </div>
<input type=”text” name=”address_line_2″ size=”20″ “/> <br />
<div>City: </div>
<input type=”text” name=”city” size=”20″ /> <br />
<div>Postcode: </div>
<input type=”text” name=”postcode” size=”20″ /> <br />
<div>Phone Number: </div>
<input type=”text” name=”phone” size=”20″ /> <br />
<br />
–>
<input type=”submit” name=”submit” value=”Submit” />

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

[/CODE]

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@FangOct 23.2008 — &lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Form&lt;/title&gt;
&lt;style type="text/css"&gt;
input {margin:5px}
div
{
width:150px;
float:left;
overflow:hidden;
}
&lt;/style&gt;
&lt;script type="text/javascript"&gt;
&lt;!--
function validate( form )
{
var regexpstr=/^[a-zA-Z]+$/;

var fname=form.first_name.value;
var error=document.getElementById('error');
if(fname==" "||fname==""){
error.innerHTML="Please enter your first name";
return false;
}
else if(fname.match(regexpstr)){
error.innerHTML='';
alert("OK");
return true;
}
else{
error.innerHTML="Please check if you have entered your first name correctly";
return false;
}
}
--&gt;
&lt;/script&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;form onsubmit="return validate(this)"&gt;
&lt;p id="error"&gt;&lt;/p&gt;
&lt;div&gt;First Name: &lt;/div&gt;
&lt;input type="text" name="first_name" size="11" /&gt; &lt;br /&gt;
&lt;!--
&lt;div&gt;Surname: &lt;/div&gt;
&lt;input type="text" name="surname" size="11" /&gt; &lt;br /&gt;
&lt;div&gt;Address Line 1:&lt;/div&gt; <br/>
&lt;input type="text" name="address_line_1" size="20" /&gt; &lt;br /&gt;
&lt;div&gt;Address Line 2: &lt;/div&gt;
&lt;input type="text" name="address_line_2" size="20" "/&gt; &lt;br /&gt;
&lt;div&gt;City: &lt;/div&gt;
&lt;input type="text" name="city" size="20" /&gt; &lt;br /&gt;
&lt;div&gt;Postcode: &lt;/div&gt; <br/>
&lt;input type="text" name="postcode" size="20" /&gt; &lt;br /&gt;
&lt;div&gt;Phone Number: &lt;/div&gt;
&lt;input type="text" name="phone" size="20" /&gt; &lt;br /&gt;
&lt;br /&gt;
--&gt;
&lt;input type="submit" name="submit" value="Submit" /&gt;

&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@hakzauthorOct 23.2008 — &lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Form&lt;/title&gt;
&lt;style type="text/css"&gt;
input {margin:5px}
div
{
width:150px;
float:left;
overflow:hidden;
}
&lt;/style&gt;
&lt;script type="text/javascript"&gt;
&lt;!--
function validate( form )
{
var regexpstr=/^[a-zA-Z]+$/;

var fname=form.first_name.value;
var error=document.getElementById('error');
if(fname==" "||fname==""){
error.innerHTML="Please enter your first name";
return false;
}
else if(fname.match(regexpstr)){
error.innerHTML='';
alert("OK");
return true;
}
else{
error.innerHTML="Please check if you have entered your first name correctly";
return false;
}
}
--&gt;
&lt;/script&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;form onsubmit="return validate(this)"&gt;
&lt;p id="error"&gt;&lt;/p&gt;
&lt;div&gt;First Name: &lt;/div&gt;
&lt;input type="text" name="first_name" size="11" /&gt; &lt;br /&gt;
&lt;!--
&lt;div&gt;Surname: &lt;/div&gt;
&lt;input type="text" name="surname" size="11" /&gt; &lt;br /&gt;
&lt;div&gt;Address Line 1:&lt;/div&gt; <br/>
&lt;input type="text" name="address_line_1" size="20" /&gt; &lt;br /&gt;
&lt;div&gt;Address Line 2: &lt;/div&gt;
&lt;input type="text" name="address_line_2" size="20" "/&gt; &lt;br /&gt;
&lt;div&gt;City: &lt;/div&gt;
&lt;input type="text" name="city" size="20" /&gt; &lt;br /&gt;
&lt;div&gt;Postcode: &lt;/div&gt; <br/>
&lt;input type="text" name="postcode" size="20" /&gt; &lt;br /&gt;
&lt;div&gt;Phone Number: &lt;/div&gt;
&lt;input type="text" name="phone" size="20" /&gt; &lt;br /&gt;
&lt;br /&gt;
--&gt;
&lt;input type="submit" name="submit" value="Submit" /&gt;

&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
[/QUOTE]


Hi, i've implemented that into my code (added other text boxes and validated them, however, when I press submit with an error, the message appears at the top as it should, but for like a second or less, and then dissappears along with the writing inside the text box. ?

I am returning false everytime a box doesn't validate
Copy linkTweet thisAlerts:
@FangOct 24.2008 — The code works as given. If you have added or changed anything in the document then you will have to post the new document.
×

Success!

Help @hakz spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...