/    Sign up×
Community /Pin to ProfileBookmark

Validate that Init Val has Changed

Hi there,

Can anyone help me with this.

I have a text field Date with the initial value being 00/00/00
On submit I want make sure they have entered the date they want and and have not just left it as 00/00/00

Also need email validation, name (must be more than 3 characters) and telephone.

I usually use dreamweavers ones but the code looks dirty and I have seen simpler versions around that do the job just as well. Cant sem to find them right now though.

Thanks for your help

Andy

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@VladdyNov 11.2004 — HTML
<form onsubmit="validate(this)">
....

JS
function validate(form)
{ var valid = true;
var dateString = form.elements["date"].value;
var eMail = form.elements["email"].value;
/* Do your validation here */
retutn valid;
}
Copy linkTweet thisAlerts:
@aorchardauthorNov 11.2004 — not sure what to add here

/* Do your validation here */

my javascript skills are pretty bad
Copy linkTweet thisAlerts:
@CharlesNov 11.2004 — [font=monospace]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"

"http://www.w3.org/TR/html4/strict.dtd">

<html lang="en">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<meta name="Content-Script-Type" content="text/javascript">

<meta name="Content-Style-Type" content="text/css">

<title>Example</title>

<style type="text/css">

<!--

form {width:17em}

fieldset {padding:1ex}

label, button {display:block}

label {text-align:right}

button {margin:auto; margin-top:1em}

-->

</style>

<script type="text/javascript">

<!--

Date.prototype.toDDMMYYYYString = function () {return isNaN (this) ? 'NaN' : [this.getDate() > 9 ? this.getDate() : '0' + this.getDate(), this.getMonth() > 8 ? this.getMonth() + 1 : '0' + (this.getMonth() + 1), this.getFullYear()].join('/')}

Date.fromDDMMYYYY = function (s) {return (/^(dd?)D(dd?)D(d{4})$/).test(s) ? new Date(RegExp.$3, RegExp.$2 - 1, RegExp.$1) : new Date (s)}

String.prototype.isDate = function () {return this == Date.fromDDMMYYYY(this).toDDMMYYYYString()}

String.prototype.isEmailAddress = function () {return /^[^x00-x20()<>@,;:".[]x7f-xff]+(?:.[^x00-x20()<>@,;:".[]x7f-xff]+)*@[^x00-x20()<>@,;:".[]x7f-xff]+(?:.[^x00-x20()<>@,;:".[]x7f-xff]+)+$/.test(this)}

function PhoneNumber (s) {this.number = s.replace(/^[01]/, '').replace (/([a-pr-y])/gi, function () {return {a:2, b:2, c:2, d:3, e:3, f:3, g:4, h:4, i:4, j:5, k:5, l:5, m:6, n:6, o:6, p:7, r:7, s:7, t:8, u:8, v:8, w:9, x:9, y:9}[arguments[1].toLowerCase()]}).match(/d/g)}

PhoneNumber.prototype.toString = function () {return [this.number[0], this.number[1], this.number[2], '-', this.number[3], this.number[4], this.number[5], '-', this.number[6], this.number[7], this.number[8], this.number[9]].join('')}

String.prototype.isPhoneNumber = function () {return /d{3}-d{3}-d{4}/.test(this)}

function check (f) {

var e, i = 0;

while (e = f.elements[i++]) {if (!/S/.test (e.value) || e.value == 'dd/mm/yyyy') {alert (['Field', e.previousSibling.data, 'is required.'].join(' ')); return false}}

}

// -->

</script>

</head>

<body>

<form action="some-script.pl" onsubmit="return check (this)">

<fieldset>

<legend>Example</legend>

<label>Date <input onchange="this.value = Date.fromDDMMYYYY(this.value).toDDMMYYYYString(); if (this.value == 'NaN') {alert (); this.value = ''; this.focus()}" onfocus="this.value = ''; this.onfocus = function () {}" name="date" type="text" value="dd/mm/yyyy"></label>

<label>E-mail address <input onchange="if (!this.value.isEmailAddress()) {alert (); this.value = ''; this.focus()}" name="email" type="text"></label>

<label>Phone number <input onchange="this.value = new PhoneNumber (this.value); if (!this.value.isPhoneNumber()) {alert (); this.value = ''; this.focus()}" name="phone" type="text"></label>

<button type="submit">Submit</button>

</fieldset>

</form>

</body>

</html>[/font]
Copy linkTweet thisAlerts:
@aorchardauthorNov 11.2004 — charles

didnt realise you could do <label> nor css a rounded box

been web designing for 3 years.

learn something new each day

thanks for your help buddy
Copy linkTweet thisAlerts:
@CharlesNov 11.2004 — [i]Originally posted by aorchard [/i]

[B[I]didnt realise you could do <label> nor css a rounded box[.][/B][/QUOTE]
Read, mark, learn and inwardly digest http://www.w3.org/TR/html401/ .



The LABEL element is very important for people who are using non-visual browsers.
×

Success!

Help @aorchard 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 6.17,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

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