Beruflich Dokumente
Kultur Dokumente
01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html lang="en_GB">
<head>
<style type="text/css"
media="print">#container{width:768px}body{margin:0;border:none;width:768px;font-
size:12px}#content{width:728px}.noprint{display:none}</style>
<script type="text/javascript">if(self!
=top)top.location.href=self.location.href;window.onload=function()
{buildMenu('submenu','h2',false);}</script>
</head>
<body>
<a class="chirplink" href="#content">skip navigation</a>
<tr>
<td id="header">
<ul>
</ul>
</div>
</td>
</tr>
<td id="content">
<div class="noprint" style="margin: 0.5em 0;">
<script type="text/javascript">google_ad_client="pub-
9789897350102210";google_ad_slot="6258627248";google_ad_width=728;google_ad_height=90;</scri
pt>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
</div>
the web browser. It can prevent people from leaving fields blank, from
<p>In the following example, the single input box, named <tt>input</tt>,
spaces. Only if both tests are passed can the form be submitted.</p>
<code><script type="text/javascript">
function checkForm(form)
if(form.<i>inputfield</i>.value == '') {
form.<i>inputfield</i>.focus();
if(!re.test(form.<i>inputfield</i>.value)) {
form.<i>inputfield</i>.focus();
</script>
</code>
is blank.</p>
<tt>false</tt> value will block the form from being submitting. The
<p>You can test the above script with different input values using this
form:</p>
<fieldset>
<input type="submit">
</fieldset>
</form>
</form>
</code>
that field from within the <tt>checkForm</tt> function. With the advent
of <a href="http://www.google.com/search?q=define:DHTML" target="_blank">DHTML</a> it's
tempting to use <tt>id</tt>'s to
reference form fields, but that can lead to namespace conflicts and why
itself (the form) as the only variable. This makes the value of the
(the 'value' of the field called 'input' belonging to the form). Other
form values are available using a similar syntax, although this becomes
<p>In a real-life situation you will most likely have more fields to
check, and more complicated conditions, but the principle remains the
if(!<i>condition1</i>) {
form.<i>fieldname</i>.focus();
return false;
if(!<i>condition2</i>) {
form.<i>fieldname</i>.focus();
return false;
...
return true;
}</code>
second condition will not be tested and so forth. Only when <b>all</b>
<p>You'll see that the all validation scripts presented on this and
<h3>Text/Textarea/Password boxes</h3>
<code>form.<i>fieldname</i>.value</code>
<p>To check whether two inputs have the same is quite simple:</p>
<code>if(form.<i>field1</i>.value == form.<i>field2</i>.value) {
}</code>
<p>and to see if they have different values we just reverse the logic:</p>
<code>if(form.<i>field1</i>.value != form.<i>field2</i>.value) {
}</code>
<p>If you want to test numeric values (or add or subtract them) then you
first have to convert them from strings to numbers. By default all form
}</code>
<h3>Select/Combo/Drop-down boxes</h3>
selectBox.options[selectBox.selectedIndex].value
selectBox.options[selectBox.selectedIndex].text</code>
relationship:</p>
<p>If you define a <tt>value</tt> for the OPTION elements in your SELECT
Internet Explorer allows you to access the value of the selected option
using the same format as for text boxes above. This is wrong!</small></p>
<p>If you just want to check that an option has been chosen (ie. that
the SELECT box is no longer in it's default state) then you can use:</p>
// no option selected
</code>
<h3>Checkboxes</h3>
<code>form.<i>checkboxfield</i>.checked</code>
if(form.<i>checkboxfield</i>.checked) {
} else {
return false;
</code>
<p><small><b>Note:</b> You don't need to test using
boolean.</small></p>
return false;}</script>
<fieldset>
</fieldset>
</form>
<h3>Radio buttons</h3>
To find out which value (if any) has been selected, you need to loop
through the array until you find which one has been selected:</p>
}
return false;
}</code>
if(radioValue = checkRadio(form.<i>radiofield</i>)) {
return true;
} else {
return false;
}</code>
return false;}
function checkForm3(form)
return false;}</script>
<fieldset>
<input type="radio" name="radiofield" value="Red"> Red
<input type="submit">
</fieldset>
</form>
<h2>Checkbox arrays</h2>
server-side script then you might already have some grey hairs from
array are often set to the same value: a name ending with <tt>[]</tt>.
return retval;}
function checkForm4(form)
return false;}</script>
<fieldset>
<p><input type="submit"></p>
</fieldset>
</form>
<p>When you submit the form you will be notified through an alert
message how many items you checked, and what they were. This is
var el = form.elements[i];
retval.push(el.value);
return retval;
</code>
<p>The form handler that calls this function and displays the alerts is
as follows:</p>
if(itemsChecked.length > 0) {
return false;
</code>
select values.</p>
<p>Normally you would modify this so that you could submit or not submit
the form based on the number of items selected. For example "at
simple exercise.</p>
<p>In more complicated forms you will want to set conditions on the form
that combine multiple elements. For example, a text input that only
<code>function checkForm(form)
...
form.<i>textinputname</i>.focus();
return false;
...
}</code>
<code>function checkForm(form)
...
switch(radioValue)
{
case 'Red':
break;
case 'Blue':
break;
default:
...
}</code>
<p>Using simple logical operators and the functions supplied above you
<h2>Related Articles</h2>
<ul>
<li><b>JavaScript: Form Validation</b></li>
</ul>
<h2>References</h2>
<ul>
<li><a
href="http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:RegExp"
target="_blank">Core JavaScript 1.5 Reference:Global Objects:RegExp</a></li>
<li><a href="http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Objects:Array"
target="_blank">Core JavaScript 1.5 Reference:Global Objects:Array</a></li>
</ul>
<div class="noprint">
<a class="addthis_button_tweet"></a>
<a href="http://www.addthis.com/bookmark.php?v=250&username=xa-4c878e016b60dae8"
class="addthis_button_compact">Share</a>
</div>
</div>
<p class="comment">Your highlighting of search terms is ANNOYING. The fact that you set a cookie with
it is ridiculous. I had to clear private data (firefox) to get rid of it. Please add a link to kill the highlighting,
or do something different so a page refresh can get rid of it.</p>
<div class="response"><p>It's not a cookie, but your cache, and you're welcome <img
src="data:image/gif;base64,R0lGODlhDwAPALMNAP/qAEVFRQAAAP/OAP/JAP6dAP+0AP/
+k//9E///x//////lAP//6wAAAAAAAAAAACH5BAEAAA0ALAAAAAAPAA8AAARZsEkZap24VZbOGRcWcAgCn
EMRTEFnnnC6ascLx2sr7DvA76rAgacQ+HZFQ+VwNJ54BGXAdoNFK4PqbUFQabJOmIAAogwGi99YxjoT3m+
2KGCoG4IZlmU2iQAAOw==" width="15" height="15" border="0" alt=""></p></div>
<br>
<p class="comment">Hi - Is it possible to have a checkbox, that has a default of checked, so that the
user HAS to enter info into the textarea. <br>
But if the user unchecks the box, the user DOESNT have to enter info in textarea. <br>
<div class="response"><p>I've added some code for this in the section "Combining Form Elements
in Conditions" <img
src="data:image/gif;base64,R0lGODlhDwAPALMNAP/qAEVFRQAAAP/OAP/JAP6dAP+0AP/
+k//9E///x//////lAP//6wAAAAAAAAAAACH5BAEAAA0ALAAAAAAPAA8AAARZsEkZap24VZbOGRcWcAgCn
EMRTEFnnnC6ascLx2sr7DvA76rAgacQ+HZFQ+VwNJ54BGXAdoNFK4PqbUFQabJOmIAAogwGi99YxjoT3m+
2KGCoG4IZlmU2iQAAOw==" width="15" height="15" border="0" alt=""></p></div>
<br>
<p class="comment">Very handy, everything else I found was rubbish but this gave me exactly what I
wanted with minor modification (removed '+' character) - needed to alert user if 'username' contained a
space /anywhere/ in the field. Hope the following tags help SEO..<br>
<br>
<br>
return true;}</script>
<fieldset>
document.getElementById('captcha_code').value = '';
return false;
">refresh</a></small></span>
<label><a href="/php/captcha/">CAPTCHA</a><strong>*</strong></label><span><input
id="captcha_code" type="text" required pattern="\d{5}" name="captcha" size="6" maxlength="5"
onkeyup="this.value = this.value.replace(/[^\d]+/g, '');"> <small><- copy the digits from the image into
this box</small></span>
</fieldset>
</form>
</td>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
</div>
</td>
</tr>
<tr>
</td>
</tr>
</table>
</body>
</html>