IntelliJ Java IDE
The moose likes HTML, CSS and JavaScript and the fly likes Alert Box Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of Practical Unit Testing with TestNG and Mockito this week in the Testing forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Reply Bookmark "Alert Box" Watch "Alert Box" New topic
Author

Alert Box

Lucky Singh
Ranch Hand

Joined: Jan 19, 2004
Posts: 125
Why is the alert message not showing up?Anything wrong in the syntax?
<script language="JavaScript">
function validateForm()
{
var emptystr;
var msg = " ";
var emptyNum=0;
if( (fname.value==null)||(fname.value==" ")||isblank(fname.value) )
{
emptyNum++;
emptyStr += fname;
}
if( (lname.value==null)||(lname.value==" ")||isblank(lname.value) )
{
emptyNum++;
emptyStr += lname;
}

if(emptyNum = 1)
msg += "The following field is empty:" + emptyStr;
else if(emptyNum >1)
msg += "The following fields are empty: \n"+emptystr +"\n"
alert(msg);
return false;
}
</script>

<form name="reg" method="GET" action="Login" onSubmit="return validateForm();">
<input type ="text" name ="fname" size="10" tabindex="1">
......
lname is for last name.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15003
one problem
if(emptyNum == 1)
use should be using
document.FormName.ElementName.value;
so it works on all broswsers
where is fname and lname defined?
plus your code is going to alert if it is true
I have a meeting so I can not help you out anymore right now...
Eric
Lucky Singh
Ranch Hand

Joined: Jan 19, 2004
Posts: 125
fname and lname are defined in the form.
Didn't understand where I should use document.formname.....
Basically I am setting the string msg to some value and displaying it in the end.
Winston Smith
Ranch Hand

Joined: Jun 06, 2003
Posts: 136
Everywhere you reference the form field, you should use the syntax:
document.FormName.FormField.
So, for instance, when you want to reference the lname field, your code should read:
document.reg.lname
To set the value:
document.reg.lname.value="Smith"
I think IE will spoil you, and let you reference fields by fieldname only, but don't get spoiled because you'll have problems when your page displays in other browsers.
[ April 02, 2004: Message edited by: Winston Smith ]

for (int i = today; i < endOfTime; i++) { code(); }
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15003
This is to give you an idea...

[ April 02, 2004: Message edited by: Eric Pascarello ]
 
 
subject: Alert Box
 
Threads others viewed
Data truncation: Truncated incorrect DOUBLE value:
How to rendar data from new window to old window through window.focus() ? Or any other way.
Resetting a Form
XMLHtmlRequest return Undefine
Check if an email-adress is valid with javascript
IntelliJ Java IDE

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture