Completing the Login Script for a PEAR CMS - JavaScript
(Page 2 of 4 )
The JavaScript code is responsible for ensuring that the username and password fields are filled in when the user submits the form. It is sometimes better to have these kinds of checks on the client side rather than on the server side, where the form data is send to the server for processing and then returned to the browser to show the user the results.
The JavaScript checks the form data locally before it goes to the server. The JavaScript code declares and implements one function called checkform(). This function takes one argument, which is the name of the form.
<script language="javascript" type="text/javascript">
function checkform(pform1){
The first part of the function checks to see if the username field has been filled in. If not, it alerts the user of that fact and sets the focus back on that field.
if(pform1.uname.value==""){
alert("Please enter a username")
pform1.uname.focus()
return false
}
The second part of the function checks to see if the password field is filled in, otherwise it shows an alert:
if(pform1.upass.value==""){
alert("Please enter a password")
pform1.pw.focus()
return false
}
The final part of the function checks to see if both the username and password has been filled in; again, it shows the user a error dialog if the information is not filled in.
if(pform1.pw.value=="" && pform1.uname.value==""){
alert("Please make sure that you have entered your username and password")
return false
}
return true
}
Next: The logout script >>
More PEAR Articles Articles
More By David Web