/    Sign up×
Community /Pin to ProfileBookmark

validation with alert boxes

Is is possible to do “in form” validation without the alert boxes? What I’d like to do is give the user a message above or below the offending input areas.

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@steelersfan88Jul 10.2004 — Along the lines of:[code=php]<script type="text/javascript">

function checkForm(frm) {
var uName = frm.elements[0].value
var age = frm.elements[1].value
var ctry = frm.elements[2].value
var ret = true
if(uName.split(', ').length != 2) {
document.getElementById('prob0').innerHTML = "Incorrect Name!"
ret = false
}
if(isNaN(age) || age == 0) {
document.getElementById('prob1').innerHTML = "Must be Numeric!"
ret = false
}
if(ctry != "US") {
document.getElementById('prob2').innerHTML = "US Born Only!"
ret = false
}
return ret;
}

</script>

<div id="form" style="float:left;text-align:center">
<form name="myForm" onsubmit="return checkForm(this)">
<div id="obj" style="height:30px;float:left;">Name (Last, First): <input type="text" name="uName"></div><BR><BR>
<div id="obj" style="height:30px;float:left;">Age: <input type="text" name="uName"></div><BR><BR>
<div id="obj" style="height:30px;float:left;">Country: <input type="text" name="uName"></div><BR><BR>
<input type="submit">
</form>
</div>
<div id="spc" style="width:35px;float:left"> </div>
<div id="checks" style="float:left">
<div id="prob0" style="color:red;font-weight:bold;height:30px;float:left;"> </div><BR><BR>
<div id="prob1" style="color:red;font-weight:bold;height:30px;float:left;"> </div><BR><BR>
<div id="prob2" style="color:red;font-weight:bold;height:30px;float:left;"> </div><BR><BR>
</div>[/code]
The CSS could be a lot better ... thats what happens in the early morning ?

Dr. Script
Copy linkTweet thisAlerts:
@keithh0427authorJul 10.2004 — Thanks for the quick reply. The problem though is that it needs to be above or below the input. I have very little room for flexibility because the form is on a side panel.

I've thought about displaying some "White" text below the input and then calling a routing to change it to "Red", just cannot figure out how to do it.
Copy linkTweet thisAlerts:
@NaemoJul 10.2004 — This code checks the value of the field. If nothing is entered then the color of the span is changed to red.

[code=php]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Untitled</title>
<script language="JavaScript" type="text/javascript">
<!--
function checkForm(){
var check=true;
if(document.myForm.info_name.value==""){
document.getElementById("val_name").style.color="#ff0000";
check=false;
}
return check;
}
//-->
</script>

</head>
<body>

<form name="myForm" onsubmit="return checkForm(this)">
Name: <input type="text" name="info_name"><br />
<span id="val_name" style="color:#ffffff">Please enter name</span><br />

</form>

</body>
</html>
[/code]


Cheers
Copy linkTweet thisAlerts:
@steelersfan88Jul 10.2004 — Noo, you'd rather change the visibility or display properties. You wouldn't want a user finding the text when they aren't supposed to. Also, the doctype you have is not complete.[code=php]<html>
<head>
<title>Untitled</title>
<script type="text/javascript">
<!--
function checkForm(frm) {
var toGo=true;
if(!frm.info_name.value.length) {
document.getElementById("val_name").style.visibility="visible";
toGo=false;

}
return toGo;
}
//-->
</script>

</head>
<body>

<form name="myForm" onsubmit="return checkForm(this)">
Name: <input type="text" name="info_name">
<button type="submit">Submit</button><br />
<span id="val_name" style="visibility:hidden">Please enter name</span><br />

</form>

</body>
</html>[/code]
for example. Dr. Script
Copy linkTweet thisAlerts:
@keithh0427authorJul 11.2004 — I apologize for late thank you to all who responded. Our doughter just provided us with our 4th grandson and I got a little busy. Thanks to everyone for helping me out on this.
Copy linkTweet thisAlerts:
@steelersfan88Jul 11.2004 — [i]Originally posted by fredmv [/i]

[B]Global solution: <http://www.peterbailey.net/fValidate/>. [/B][/QUOTE]
But that takes all the fun away from doing the work by one's self! 'Tis great for beginners to the language though.
×

Success!

Help @keithh0427 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.1,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...