Menu
Hi,
I wrote a function that that is supposed to check a form. But it doesn’t. Where is the error ?
function checkJobApplication(application) {
if (application.position.value = “”) ) {
alert(“Please enter the desired position !”);
application.position.focus();
return false;
} else if
…
}
The function is called like this:
<form action=”career.php” method=”post” onsubmit=”return checkJobApplication(this.form)”>
Is there actually an element in the form named 'position'?
If there is try this:
[color="maroon"]document.application.position.value = ""[/color] [/QUOTE]
Then, you will get an error on that line instead of the script continuing on its merry way. On one of my very first programs, I spent about 8 hours tracking down an '=' versus '==' mistake.[/QUOTE]
Better yet, use [font=monospace]if (/S/.test (application.position.value))[/font] to test for the existance of at least one non-white space character.[/QUOTE]
Thank you. Now it works for some reason.[/quote]
Would it be better to have a function for every field and then call a function application(application) from the form, so that the function application calls all the required functions for the application form? Like this?
function application(application) {
checkName(application);
checkPosition(application);
}[/quote]
if (/S/.test (application.position.value))
if (!form.position.value) {
[/quote]
[i]Originally posted by Kor [/i]
[B]if (!form.position.value) {
In fact anytime you have a value for your inputs, it is ""...
You must use
if (form.position.value=='') {
or better
if (form.position.value.length==0) { [/B][/QUOTE]
[i]Originally posted by Kor [/i]Why?
[B]Not a wise condition. In fact anytime you have a value for your inputs, and that is a '' string...
You must use:
if (form.position.value=='') {
or better
if (form.position.value.length==0) { [/B][/QUOTE]
An empty string is evaluated as false.
[/quote]
What is the easiest way to test if a field contains a number ?
[/quote]
[code=php]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script language="JavaScript" type="text/JavaScript">
function isNumber(f) {
var re = /^[0-9]*$/;
if (!re.test(f.elements['txt'].value)) {
alert("Value must be all numeric characters, non numeric's removed from field!");
f.elements['txt'].value = f.elements['txt'].value.replace(/[^0-9]/g,"");
return false;
}
}
</script>
</head>
<body>
<form onsubmit="return isNumber(this)">
<input name="txt" type="text">
<input type="submit" value="Submit">
</form>
</body>
</html>
[/code]
0.1.9 — BETA 5.18