JavaScript - Code Help For Basic Email And Password Validation.
Working on college assignment, cant figure it out and need help! Basically a form that asks the user to fill in a valid email and password (ill give them the password), doesnt matter if it isnt secure, or if the email accepts dodgy emails (im just verifying the @ and a . and that is ok for the purpose of this. Anyway been trying with this code for ages now and cant see what is wrong but it isnt working, ive swapped it around and at times it does check that it is a valid email, sometimes it allows empty inputs. here is the code
<script type="text/javascript"> function validateForm() { var x=document.forms["myForm"]["email"].value var y=document.forms["myForm"]["password"].value var atpos=x.indexOf("@"); var dotpos=x.lastIndexOf("."); if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length) { alert("Not a valid e-mail address"); return false; } </script> I think i need something like && (y!="123") around the 'if' statement, but not a clue how or where it goes. Basically im going to set the password to 123 so dont have to allow for caps etc. Easiest way to do this, anyone plleeeeeaaasseee?? Kind regards Alan Similar TutorialsI am struggling trying to get this bit of code I've written to work. It should validate a password that is 10 characters long, starts with a D and includes 1 *. Even if I blank out two of the while statements and only use the charAt while statement it still fails to loop back. Any ideas? function validatePassword() { //declare and initialise variables var intCount = 0; var intCharCount = 7; var intAstrixCount =0; var strPassword = parseInt(prompt("Please enter password", "")); while (strPassword.length < intCount) { alert ("Your password must be at least 10 characters long"); strPassword = parseInt(prompt("Please enter password", "")); intCount++; } while (strPassword.charAt(0) != "a") { alert ("Your password does not start with a D"); strPassword = parseInt(prompt("Please enter password", "")); intCount++; } while (intAstrixCount != 1) { alert ("Your password does not contain an *"); strPassword = parseInt(prompt("Please enter password", "")); intCount++; } alert ("Your password is correct"); } Hi. Firstly I must point out, although I know this probably isnt the best way to create a login system. Its just an example for some of my school work. Basically, I have a very basic "bare bones" login form. What I want to be able to do, is to create an IF statement that will allow only 1 pair of values for the username and password. eg. username = john password = smith now, I want the if statement to run, and look for the values entered in the username and password fields. if the value for username is correct, and the password is correct, i want another page to open, however if any other values are entered, i want an alert message to be thrown. "please enter correct login details". My problem with this, Is i do not know how to write an if statement that will except one value, and reject all others. Iv included a printscreen of my coding and I warn you, it may be very wrong, but Im confused as to how to make it work. Heres the image of my code: http://i39.tinypic.com/e8sb9j.png Thanks for your time. function StrongPassword(Input) { //var re = /(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,}/; //1 number,1 Uppercase,1 lowercase,6 characters var re = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])/; //1 digit,1 Uppercase,1 lowercase if (re.test(Input)) { alert('Strong Password'); } else { alert('Weak pwd'); } } Help to get correct output.... both the expr will execute the else block only... Hello, I am a computing pupil currently taking part in a computer science project at GCSE level. Our project is a research task regarding web form validation, JavaScript and embedding JavaScript into HTML code. I have no prior knowledge of any of these topics, so would be very interested in hearing your opinions on the aboce. I would also like to know your opinion on how effective JavaScript validation is, possibly in your field of work, or just your experience of using it? Thanks in advance, Ellen Let's start by saying that I stink at javascript. I've taken multiple different tutorials, some more than once, and I still can't get it. So, when I need a code, I search for it, and try different ones until I find something that works. I've been looking for a code for email validation for quite a long time, and I still can't find something that works with my code for validating the other parts of my contact form. I can get it to check for the presence of text, but not for the presence of an @ sign and period. I would like for someone to please help me figure this out. Right now I have the following code: Javascript Code: <!-- function validate_form ( ) { valid = true; if ( document.contact.firstnamecontact.value == "" ) { alert ( "Please fill in the 'First Name' box." ); valid = false; } if ( document.contact.lastnamecontact.value == "" ) { alert ( "Please fill in the 'Last Name' box." ); valid = false; } if ( document.contact.emailcontact.value == "" ) { alert ( "Please fill in a valid email address." ); valid = false; } if ( document.contact.subjectcontact.value == "" ) { alert ("Please fill in the 'Subject' box." ); valid = false; } if ( ( document.contact.topic[0].checked == false ) && ( document.contact.topic[1].checked == false ) && ( document.contact.topic[2].checked == false ) ) { alert ( "Please choose your Topic of Inquiry" ); valid = false; } if ( document.contact.messagecontact.value == "" ) { alert ( "Please specify a question/comment." ); valid = false; } return valid; } //--> HTML Code: <form name="contact" method="post" action="contactcompletion.php3" onsubmit="return validate_form ( );"> <table> <tr> <td><b>First Name: </td><td><input type="text" name="firstnamecontact"/></td> </tr> <tr> <td><b>Last Name: </b></td><td><input type="text" name="lastnamecontact"/></td> </tr> <tr> <td><b>Email: </b></td><td><input type="text" name="emailcontact"/></td> </tr> <tr> <td><b>Subject: </b></td><td><input type="text" name="subjectcontact"/></td> </tr> </table> <br /> <br /> <h4>Topic of Inquiry (Choose One):</h4> <table> <tr> <td><input type="radio" name="topic" value="questionscontact" /><b> Questions</b></td> <td><input type="radio" name="topic" value="commentscontact" /><b> Comments</b></td> <td><input type="radio" name="topic" value="othercontact" /><b> Other</b></td> </tr> </table> <br /> <br /> <h4>Enter Your Questions or Comments:</h4> <br /> <textarea name="messagecontact" rows="10" cols="70"></textarea> <br /> <br /> <p><input type="submit" name="send" value="Send" /></p> Thanks a bunch in advance for helping me!! I've got a form validation script and I'm trying to add in a little extra oomph to what it checks. I'm not sure on the syntax for this, but I'm currently trying to make sure the email has the proper data using something like: else if (email.indexOf("@")<1 || email.indexOf(".")==-1 || email.indexOf(",")!=-1 || email.indexOf(" ")!=-1 || email.length<6) But when I add that if statement, the script does nothing, and they're taken to the next page. While I'm at it, is it possible to make sure that at least one of a series of looped-in dropdowns that have a php generated name: <select name="participantqty[<?= $c_row['workshop_id'] ?>]" id="" class=""> Here's the script I'm working from, with my if statement in it and breaking things: Code: <script type="text/javascript"> function required(){ var first = document.forms["register1"].elements["fname"].value; var last = document.forms["register1"].elements["lname"].value; var email = document.forms["register1"].elements["email"].value; var message = document.forms["register1"].elements["phone"].value; if(first == null || first == "" || last == null || last == ""){ alert("First and last name fields are required."); return false; } else if(email == null || email == ""){ alert('An email address is required.'); return false; } else if (email.indexOf("@")<1 || email.indexOf(".")==-1 || email.indexOf(",")!=-1 || email.indexOf(" ")!=-1 || email.length<6) { alert('Please enter a valid E-mail address.\n" + "Example: myname@domainname.com"); register1.email.focus(); return false; } else if(message == null || message == ""){ alert('A phone number is required.'); return false; }else{return true;} } </script> This piece of Dreamweaver(?) code appears to validate an email address: Code: function MM_validateForm() { //v4.0 var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments; for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]); if (val) { nm=val.name; if ((val=val.value)!="") { if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@'); if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n'; } else if (test!='R') { num = parseFloat(val); if (isNaN(val)) errors+='- '+nm+' must contain a number.\n'; if (test.indexOf('inRange') != -1) { p=test.indexOf(':'); min=test.substring(8,p); max=test.substring(p+1); if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n'; } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; } } if (errors) alert('The following error(s) occurred:\n'+errors); document.MM_returnValue = (errors == ''); } The checking routine works well except I can't figure out how to invoke the email check part (red above?). This is the onsubmit bit: Code: <form id="quoteform" name="contactfrm" onsubmit="MM_validateForm('First_Name','','R','Last_Name','','R','City','','R','State','','R','Zip','','R','Day_Phone','','R','Evening_Phone','','','Email','','R');return document.MM_returnValue;" action="contact.php" method="post"> I have tried altering the field id and name below to "isEmail' but it does nothing: Code: <td height="32" bgcolor="#F8F8F8"><strong>Email Address:</td> <td><input name="YOUR EMAIL" type="text" id="Email" size="30"></td> Is it obvious? Hi Everyone! I have a text box ... but the label is in the textbox as the value so I use this code to make sure that something is filled in the field when a user clicks send: if(form.email.value == ('' || 'email*')){ alert('Please enter your email address'); return false; } <label><input type="text" name="email" id="email" class="required" value="email*" onfocus="if( this.value == 'email*') {this.value = '';}" onblur="if (this.value == '') { this.value = 'email*';}" /></label> I have tried to add email validation to the javascript code but I am failing! can someone help please? at the moment it just checks to see if the field has been filled out but doesn't check for the @ and .co.uk or .com. Many thanks! This are my codes.. var add = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/; var email = form.txtEmail.value; if (add.test(email) = false) { alert("Please enter a valid email."); return false; } return true; } bt it wun work.. pls help.. I was hoping you could point out why the validation for the email in the script below keeps looping and how to fix it. Any pointers would be great. Thank you. Script is being used on a Static FBML page. Code: <script type="text/javascript"> function checkForm() { var params=document.getElementById('myForm').serialize(); if(params.fullname==''){ displayerrormessage('Please Enter Your Full Name'); return false; } if(params.useremail==''){ displayerrormessage('Please Enter Your Email'); return false; } if(params.useremail!=''){ var emailfilter=/^w+[+.w-]*@([w-]+.)*w+[w-]*.([a-z]{2,4}|d+)$/i var returnval=emailfilter.test(params.useremail); if (returnval==false){ displayerrormessage('Please Enter Valid Email'); params.useremail.select(); } return false; } if(params.userphone==''){ displayerrormessage('Please Enter Your Phone'); return false; } if(params.usercity==''){ displayerrormessage('Please Enter Your City'); return false; } if(params.userstate==''){ displayerrormessage('Please Enter Your State'); return false; } if(params.usercountry==''){ displayerrormessage('Please Enter Your Country'); return false; } if(params.CompanyOrganization==''){ displayerrormessage('Please Enter Your Company/Organization'); return false; } if(params.TitlePostion==''){ displayerrormessage('Please Enter Your Title/Postion'); return false; } if(params.Message==''){ displayerrormessage('Please Enter Your Message'); return false; } } function displayerrormessage(message){ new Dialog().showMessage('Erro Message',message); return false; } </script> <script type="text/javascript"> var emailfilter=/^w+[+.w-]*@([w-]+.)*w+[w-]*.([a-z]{2,4}|d+)$/i function checkmail(e){ var returnval=emailfilter.test(e.value); if (returnval==true){ alert('Please enter a valid email address.') e.select() } return true; } </script> <form id="myForm"> <p>Full Name : <input type="text" name="fullname" id="fullname"/> </p> <p>Email : <input type="text" name="useremail" id="useremail"/> </p> <p>Phone : <input type="text" name="userphone" id="userphone"/> </p> <p>City : <input type="text" name="usercity" id="usercity"/> </p> <p>State : <input type="text" name="userstate" id="userstate"/> </p> <p>Country : <input type="text" name="usercountry" id="usercountry"/> </p> <p>Company/Organization : <input type="text" name="CompanyOrganization" id="CompanyOrganization"/> </p> <p>Title/Postion : <input type="text" name="TitlePostion" id="TitlePostion"/> </p> <p>Your Message : <input type="text" name="Message" id="Message"/> </p> <input type="button" name="nameuser1" id="nameuser1" onclick="checkForm();" value="Submit"/> </form> I am having trouble with my Contact Us form. For the validation for email, in function validateEmail in my javascript portion where if I include: Code: else if (fld.value(".com")==-1 && entEmail.indexOf(".net")==-1 && entEmail.indexOf(".org")==-1 && entEmail.indexOf(".edu")==-1) { window.alert("Please recheck your email address. it must in name@domain.xxx format."); return false; } It skips the validation when I click the submit button and sends without validating the form. When I remove the code snippet above it validates fine but doesn't check for ".com", ."org", or ."net" which has to be included. I need to include a proper email address that includes "@" AND either one of these: ".com", ."org", or ."net" Anyone know of any ideas to solve this? I can't seem to get any Email validation to work, at all. no matter what different tactic I try. Right now I'm trying the most simplest of tactics.. Code: if((document.subscribe.contacttype.selectedIndex==1) && (document.subscribe.email.value=="")){ alert("Please enter a Valid Email Address."); return false; } else{ if((document.subscribe.email.indexOf('@') < 0) || ((document.subscribe.email.charAt(email.length-4) != '.') && (document.subscribe.email.charAt(email.length-3) != '.'))) {alert("you have entered an invalid Email Address. Please try again."); } } } Any suggestions? my 'Email' feed is displayed after the 'drop-down selector' has been chosen, thus, the invoking of the if selectedIndex==1 && email.value="" return false;. Thanks for your assistance ahead of time. Hey, basically I have my assignment due in 2 and a half hours and I cannot work out how to validate a email address. What am I doing wrong? Been on the net for a couple of hours lurking to find some answers but now I am even more lost. Bit of information: Basically I need to work out how many tables are needed for a class room depending on the desktops and laptops used and how many roils of coils are needed blah blah blah. Then comes the tricky part (for me anyways) asking the user to confirm or cancel the order everything works up to confirming the quote and then validating the email address, canceling the confirm button works so I'm assuming it's just the validating that I have destroyed. Any help would be awesome, assignment is due very soon and no sections of code are helping me on the net. Code: function confirmquote() { var answer=confirm("Do you want to recieve a quote?"); if (answer==true) { var emailID=window.prompt("Please enter a VALID email address or type quit to exit"); } else { alert("Quote has been rejected sir, please refreash to start order agian."); } } } function CheckEmail() { emailID = document.f1.Email.value AtPos = emailID.indexOf("@") StopPos = emailID.lastIndexOf(".") Message = "" if (emailID == "") { Message = "Not a valid Email address" + "\n" } if (AtPos == -1 || StopPos == -1) { Message = "Not a valid email address" } if (StopPos < AtPos) { Message = "Not a valid email address" } if (StopPos - AtPos == 1) { Message = "Not a valid email address" } return Message } (ALL IN THE HEADER TAG) Code: var worknowplease = confirmquote(); var emailID=checkemail(); (Last few lines of code before [/body]) I have no idea what the validate email is trying to do in the coding, but I'm still very freash on JS. Thanks you for reading and if you can share any possible solutions to my problems. Thanks agian. Hello, When I have email validation and captcha in the same form, simply passing the captcha allows the form to be sent without an email address. When I remove the captcha, the form will not send without the email address. I want to have both. What am I missing? http://jaxpubliclibrary.org/lib/websiteform-test.html Code: <script> function validateForm() { var x=document.forms["WebsiteQuestion"]["Email"].value; var atpos=x.indexOf("@"); var dotpos=x.lastIndexOf("."); if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length) { alert("Not a valid e-mail address"); return false; } } function clearForms() { var i; for (i = 0; (i < document.forms.length); i++) { document.forms[i].reset(); } } </script> Code: <div align="center"> <script language="javascript" src="http://www.captcha.cc/cap.js?cap_k=tocsoLPahVjmEHJZWtenaJEDoJXfrQaa" type="text/javascript"> </script> <input type=hidden name=cap_h id=cap_h value=""> <img border=1 id=cap_i src="http://www.captcha.cc/img.cgi?cap_k=tocsoLPahVjmEHJZWtenaJEDoJXfrQaa" align=middle><img align=middle style="margin-left: 4px;" id=cap_r onload="return cap_showReload(event);" src="http://www.captcha.cc/blank.png"><br> <input id=cap_t name=cap_t type=text value="" style="margin-top:4px;"> <input type=submit value="Submit Form" onclick="return cap_valid(event);"> </div> I appreciate any help. Thank you, Elbee This is very basic yet i can find the answer. How do you stop a countdown function?
Code: <html> <head> <title>Click the button</title> <script type="text/javascript"> function x() { window.alert('1'); window.alert('2'); window.alert('3'); } document.getElementById("y").onclick = x; </script> </head> <body> <form> <input type="button" value="Click the button" id="y" /> </form> </body> </html> By the way... Are there any good websites / validators that can be helpful in situations like that? I've been looking at this one so far: http://www.jslint.com/ Thanks. Hi All, I am trying to create a script for checking that checks that the email address entered into two input boxes is the same when a submit button is clicked, I have these two input boxes Code: <input type="text" name="user_email" id="user_email" /> <input type="text" name="user_email2" id="user_email2" /> This javascript code Code: <script type="text/javascript"> var email1 = document.getElementById(user_email); var email2 = document.getElementById(user_email2); function checkEmail(){ if (email1 != email2) { alert("The two email addresses are not the same"); } } </script> and this code for the button Code: <input type="submit" name="submit" id="submit" onSubmit="checkEmail" /> However this code is not working, can anyone see where I am going wrong? Any help will be appreciated Thanks in advance I have been modifying this form: http://www.kartaway.com.au/form.html to be part of an iPhone optimized site for the same client. The modified form works, however it no longer stops emails being sent that do not have the required fields filled in. The modified form is he http://marketingandbranding.com.au/itest/icontact.html The original is built using a table which I have removed and replaced with divs. Didn't think I'd left anything out which would matter but I guess I must have. Please help, i don't really know any JavaScript except what I copy and paste. Hello all, I am looking to add an email validation to my new user registration form. Currently, we do not validate (or activate) a new user via an activation link in the email. We don't have that process. I'd like to get my hands on the code to randonly create a security code and the actual code to sent the validation email. Can anyone help me out here? I'm absolutely new to PHP or ASP coding. I do know some HTML. Thanks
Hi, I have a simple bit of HTML / Javascript to validate email addresses entered into a field, split by a comma. Code: <html> <body> <form name="myForm" onsubmit="return chkEmail()" method="post"> e: <input type="text" name="fname"> <input type="submit" value="Submit"> </form> </body> </html> Code: function validateEmail(email) { var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; return re.test(email); } function chkEmail(){ var myString = document.forms["myForm"]["fname"].value; var mySplitResult = myString.split(","); for(i = 0; i < mySplitResult.length; i++){ emailCheck = validateEmail(mySplitResult[i]); alert(mySplitResult[i] + " - " + emailCheck); if (emailCheck = 'false') { alert(emailCheck); } } } If I enter e.g. "you@me.com,this@that.com" into the form, the alert debugs show: First: you@me.com - true Second: false Third: this@that.com - true Fourth: false The first debug seems to show that the condition is true, but then the if statement to check for a "false" value is firing, showing the value of "emailCheck" = "false", even though on the preceding alert, the value = "true". I'm not sure what's going wrong. Apologies for any silly mistakes. I also put my code he http://jsfiddle.net/XYyZa/ Thanks |