Adding JavaScript to an existing file?
-
I have an existing php page that I need to add JavaScript code to to make the zip code field numeric only and I have to have it throw an alert message when it's not numeric. I think I need to use the isNaN() function from JavaScript, but I don't know how to write it into my existing document. Can anyone help? I have pasted my existing php file here so you can see the code. Please help me. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml… <html xmlns="http://www.w3.org/1999/xhtml"> <html> <head> <script type="text/javascript"> <!-- function validate( ) { // Set the text for the error message error_message = "The following form fields are required: \n\n"; return_value = true; if ( document.signup.first.value == "" ) { error_message = error_message + "First Name \n"; return_value = false; } if ( document.signup.last.value == "" ) { error_message = error_message + "Last Name \n"; return_value = false; } if ( document.signup.email.value == "" ) { error_message = error_message + "Email \n"; return_value = false; } if ( document.signup.password.value == "" ) { error_message = error_message + "Password \n"; return_value = false; } if ( document.signup.zipcode.value == "" ) { error_message = error_message + "Zipcode \n"; return_value = false; } if ( return_value == false) { alert ( error_message ); } return return_value; } //--> </script> <title>Signup Form</title> </head> <body> <!-- Notice that the action attribute has not been specified --> <form name="signup" action="confirm.php" method="POST" onsubmit="return validate( );"> <fieldset> <legend>Signup!</legend> <label for='first'>First Name: </label><input type='text' name='first'><br/> <label for='last'>Last Name: </label><input type='text' name='last'><br/> <label for='email'>Your Email: </label><input type='text' name='email'><br/> <label for='password'>Password: </label><input type='text' name='password'><br/> <label for='zipcode'>Zipcode: </label><input type='text' name='zipcode'><br/> <input type='submit' name='submit' value='Send'> </form> </body> </html>
-
Answer:
After the following code if ( document.signup.zipcode.value == "" ) { error_message = error_message + "Zipcode \n"; return_value = false; } Add following if ( isNaN(document.signup.zipcode.value)) { error_message = error_message + "\nZip code should be a number"; return_value = false; }
Karen at Yahoo! Answers Visit the source
Related Q & A:
- How to return a value from .ashx file to javascript in a variable?Best solution by forums.asp.net
- Is there a recommended and consistent way of adding social buttons?Best solution by User Experience
- Why is Javascript called Javascript, if it has nothing to do with Java?Best solution by Stack Overflow
- How to download a file in javascript?Best solution by Geographic Information Systems
- How to get the file using it's URL in Javascript?Best solution by befused.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.