JavaScript - Checking For Uniqueness Together With Validation
Hai Friends..I made a registration form.I have used php,html as well as javascript in it.In the onclick event of submit button I've provided a call to a function written in javascript.Things are workin fine i.e validations are working properly and insertion of values to the database is also ok.
And the problem dat I have is, I want to check whether username and emailid provided is unique with respect to my site...that is no two users can't have same emailid or username.I'm able to retreive the entire emailid and username out of the database.But I don't know where should I place the code for checking the equality.I think that the code should be placed in the javascript validation function..but the code for retreiving values from db is in php. I'm totally confused.I'm attaching here the registration page that I made...anyone out there please go through it and help me with a solution. Similar TutorialsHey all. I have a simple validation I need to do. I need to just make sure that a Checkbox is checked, and that a Text field has content. Sounds simple but I cannot find any thing that has a check and a text field. Here what I have. Can I modify this script to do this? A Checkbox MUST be checked and Text field MUST be filled out. This currently does the text field fine, but no Checkbox obviously. How can I add a checkbox validation to this? Thats it. Any help is appreciated. Code: <script type="text/javascript"> var textFields = ["digsig"]; function validateForm( ) { var oops = ""; // must initialize this! var form = document.sig; for ( var t = 0; t < textFields.length; ++t ) { var field = form[textFields[t]]; var value = field.value.replace(/^\s+/,"").replace(/\s+$/,""); // trim the input if ( value.length < 1 ) { oops += "You MUST enter your Digital Signature"; } } if ( oops != "" ) { alert("ERROR:" + oops); return false; } } } </script> Hello all, new here Seems like a very nice place to be apart of. I have my website www.gebcn.com. If you view source you will see all that I have done, but more importantly my problem. I have the JS code at the top there and I am unable to W3C validate my HTML because of the JS. I am using XHTML strict and would like to stay using it. The JS I have at the top is my form validation code. I am able to do any validating that I need with this "snippet" of code, I have shrank it from my library version just to use for this newsletter. Until now W3C validating was not important now for some reason it is and I am faced with this problem. I am not a Javascript guy more of a HTML/CSS guy and I can manipulate JS to suit my needs. <problem> I have tried to make this "snippet" of JS code an external file but receive multiple errors with the JS calling for the FORM NAME as it is not on the same page. The form NAME=NEWSLETTER is another problem, as W3C says I am unable to use attribute "NAME" in this location. <problem> I would like to keep the JS close to how it is now as I have a library to use this JS over and over again. Any pointers in the right direction or solutions to my problem would be greatly appreciated. Thanks! Hopefully it is not to hard huh If there is anything anyone needs, the code pasted here, or anything else please let me know. Thanks again! I made a cool calendar application I want someone to look over it and see if I messed up anywhere cause I have a good feeling I did. It was designed to be an active desktop ^.^ that way I get my desktop image and a calendar ^.^ index.html Code: <html> <head> <title>Desktop</title> <link rel="stylesheet" type="text/css" href="css/mainTemplate.css" /> </head> <body> <img id="bgImage" src="images/nerbian-entrance.jpg" alt="Active Desktop made by Shelby Brittain" /> <table id="mainTable"> <caption id="caption"> </caption> <tr> <th> Sunday </th> <th> Monday </th> <th> Tuesday </th> <th> Wednesday </th> <th> Thursday </th> <th> Friday </th> <th> Saturday </th> </tr> <tr> <td id="d1"> </td> <td id="d2"> </td> <td id="d3"> </td> <td id="d4"> </td> <td id="d5"> </td> <td id="d6"> </td> <td id="d7"> </td> </tr> <tr> <td id="d8"> </td> <td id="d9"> </td> <td id="d10"> </td> <td id="d11"> </td> <td id="d12"> </td> <td id="d13"> </td> <td id="d14"> </td> </tr> <tr> <td id="d15"> </td> <td id="d16"> </td> <td id="d17"> </td> <td id="d18"> </td> <td id="d19"> </td> <td id="d20"> </td> <td id="d21"> </td> </tr> <tr> <td id="d22"> </td> <td id="d23"> </td> <td id="d24"> </td> <td id="d25"> </td> <td id="d26"> </td> <td id="d27"> </td> <td id="d28"> </td> </tr> <tr> <td id="d29"> </td> <td id="d30"> </td> <td id="d31"> </td> <td id="d32"> </td> <td id="d33"> </td> <td id="d34"> </td> <td id="d35"> </td> </tr> <tr> <td id="d36"> </td> <td id="d37"> </td> <td id="d38"> </td> <td id="d39"> </td> <td id="d40"> </td> <td id="d41"> </td> <td id="d42"> </td> </tr> </table> </div> <script type="text/javascript" src="js/mainLib.js"> //<![CDATA[ //]]> </script> </body> </html> css/mainTemplate.css Code: body { background-color: black; color: #C0C0FF; } caption { font-size: 24pt; } th, td { width: 100px; border: 1px solid #C0C0FF; } th { height: 25px; } td { vertical-align: bottom; text-align: right; } #bgImage { position: absolute; top: 0px; left: 0px; width: 102%; height: 103%; } #mainTable { position: absolute; top: 2%; left: 61%; border: 1px solid #C0C0FF; padding: 5px; vertical-align: top; width: 600px; height: 375px; background-color: black; opacity:0.5; filter:alpha(opacity=50); } js/mainLib.js Code: var d = new Date(); var day = d.getDate(); var wk = d.getDay(); var set = new Array(35); var j = 0; var mnth = d.getMonth(); function getMonthName() { var mth = ""; var month = new Array(12); month[0]="January"; month[1]="Febuary"; month[2]="March"; month[3]="April"; month[4]="May"; month[5]="June"; month[6]="July"; month[7]="August"; month[8]="September"; month[9]="October"; month[10]="November"; month[11]="December"; mth = month[d.getMonth()]; return mth; } function getFirstDay() { var firstDay = wk; for(var i=day-1; i>=0; i--) { firstDay--; if(firstDay == -1) { firstDay=6; } } return firstDay; } if(mnth==1 && getFirstDay()!=6) { monthLength = 28; } else if(mnth==1 && getFirstDay()==6) { monthLength = 28+6; } else if(mnth==0 || mnth==3 || mnth==5 || mnth==8 || mnth==10) { monthLength = 30+getFirstDay(); } else { monthLength = 31+getFirstDay(); } document.getElementById("caption").innerHTML = getMonthName(); if(getFirstDay()==6) { for(var i=1; i<=monthLength-6; i++) { j++; set[i] = "d"+ (i); document.getElementById(set[i]).innerHTML = j; } } else { for(var i=getFirstDay()+1; i<=monthLength; i++) { j++; set[i] = "d"+ (i + 1); document.getElementById(set[i]).innerHTML = j; } } Thanks Shelby hello, This seems very basic, but I am running into a complication. What I would like to do is check if a certain div is displayed or hidden (really doesnt matter which) then set a map zoom level based upon that. here is my current function. function closeDirections(){ gdir.clear(); if(document.getElementById("river").style.display="none"){ map.setZoom(16); hideID('river'); showID('main'); } if(document.getElementById("main").style.display="none"){ map.setZoom(12); hideID('main'); showID('river'); } } Also here is a link to my map, just in case. map I have tried several variations, but from what I can tell with every instance, it only examines the first and never considers the second "if". I thought to rememdy this issue by using if/else...same issue. The only thing I could think is a nested if statements, but I am not sure what to use as a parent if. I have searched for a while, but cannot seem to find something like an .is_hidden(). Something that I could see if its true/false. Any help would be very much appreciated. I need to check to see if a cookie has been previously created: Code: var startnum = readCookie('a1'); if (document.cookie.indexOf('a1') == -1){ var startnum = "0"; } Should line 2 be: Code: if (document.cookie.indexOf('a1') == null){ ? Does == -1 do the same thing? I keep getting myself into trouble because I tend to use my Java reference when coding Javascript, and of course Javascript doesn't support all Java functions. I've just discovered that apparently Javascript doesn't support the trim() function. But curiously, if I code it as: mystring.trim() then Firefox will handle it without any errors. (I haven't actually checked to see if whitespace is removed from around the string.) But when I code it like this: trim(mystring) then firefox gives an error. I've also found a couple of situations where I've made syntax errors, and Firefox gives no error message. This makes debugging tricky, since the only resource I have available is Firefox's Error Console. Safari has no error reporting whatsoever, AFAIK. MSIE seems to find some errors that Firefox misses, but I don't have MSIE on my main computer, so it's a nuisance to use. As a hobbyist, I have no budget to buy Javascript development tools. Is there something free available that does a better job of finding errors? Hello all I'm trying to get this function to check for 10 digits only but alert shows regardless of less / more or exactly 10 digits --- what am I missing? any help on sorting this greatly appreciated. Code: function validate(form) { if (document.forms[0].callme.checked) { var phone = document.forms[0].mobile.value; // assign value to var phone = phone.replace( /[^\d]/g, "" ); // zap all non-digit characters if ( document.forms[0].mobile.length != 10 ) // chk for 10 digits{ alert("Enter 10 numbers"); return false; } else { alert("Yahoo"); } } low tech Hi. I'm having some trouble with validating all my fields in my form. I'm posting my code here. Code: function validateForm() {var chk = document.forms["EntryPage"]["studentname"].value; if (chk==null || chksdtnm=="") { alert("Student name must be filled out"); return false; } if (chk==null || chksdtnm=="") { alert("Student ID must be filled out"); return false; } } I need to alert the user with a messagebox for six fields if ever they're not filled in. How do I achieve that? Hey guys. I need a little help with checking elements in my array. I created an array of size 50 and initialized all elements to 1. What i want to do is if the user types in the number "1" into the prompt box, i want it to check the first 15 elements and increase only one of the elements by 1, and if the element has already been increased by 1 then i want it to check the next element and see if its been increased, if the element has been increased then it needs to move on to the next one and check that. If the element is 1, then it can be increased by "1", if the element is 2 then i need it to check the next element. If all elements within the first 15 have increased by 1, it should just alert the user that all slots have been assigned. Is there a way to do this? Thanks for your time
Hi all, I have this code in javascript that is basically a number base converter and it works fine, all I want to do is add error checking, and I think it would probably be done with if statements?? But I'm not sure where they should go or how to write one, but I know all it will need is basically to say all numbers from 0-9 and all letters from A-F are ok and for the letters to be case insensitive, does that need an "i" flag or something? Anything else which someone tries to put in I want an alert to appear to say like invalid input or something, can someone help please?? Here is my code: Code: </script> <table cellpadding=5px> <tr> <td> Input Number: <textarea rows="2" name="inbox" cols="20" wrap="virtual"></textarea><br> </td> <td> Input base: <select name=from> <option name= dec value=10>Decimal</option> <option name= bin value=2>Binary</option> <option name= hex value=16>Hexidecimal</option> <option name= oct value=8>Octal</option> </select> </td> </tr> <tr> <td> Output Number: <textarea rows="2" name="outbox" cols="20" wrap="virtual"></textarea><br> </td> <td> <br> Output base <select name=to> <option name= bin value=2>Binary</option> <option name= hex value=16>Hexidecimal</option> <option name= oct value=8>Octal</option> <option name= dec value=10>Decimal</option> </select> </td> </tr> <tr> <td> <input type="button" value="Convert" onclick="javascript:convert();"> </td> </tr> </table> </form> Thanks to all who help Hi all. This is a generic question as Google didn't help at all, does anyone know how to check if javascript is enabled and then I want to display info based on that. I don't mind how I do it, HTML, PHP or whatever Regards, Magnetica I am using the following script on an e-commerce site to ensure that not both drop downs cannot be selected at the same time, as well as ensuring one must be selected at all times. Code: <script language="javascript"><!-- function validate(cart_quantity) { if (((cart_quantity.elements['id[2]'].value == 9999) && (cart_quantity.elements['id[3]'].value == 9999)) || ((cart_quantity.elements['id[2]'].value != 9999) && (cart_quantity.elements['id[3]'].value != 9999))) { alert('Please select only one option!'); cart_quantity.elements['id[2]'].value = 9999; cart_quantity.elements['id[3]'].value = 9999; cart_quantity.elements['id[2]'].focus(); return false; } } //--></script> The initial value of the drop down is set to 9999 off the bat. The problem with this code is that not all of my product pages have these 2 drop downs so I wanted to try and add in an if statement saying that if id2=9999 and id3 == null then pop up the error. That didnt work because I don't think it can be equal to null if it does not exist on the page. So instead I tried to do the following: Code: <script language="javascript"><!-- function validate(cart_quantity) { cart_quantity.elements['id[3]'] = 99999; if (((cart_quantity.elements['id[2]'].value == 9999) && (cart_quantity.elements['id[3]'].value == 9999)) || ((cart_quantity.elements['id[2]'].value != 9999) && (cart_quantity.elements['id[3]'].value != 9999))) { alert('Please select only one option!'); cart_quantity.elements['id[2]'].value = 9999; cart_quantity.elements['id[3]'].value = 9999; cart_quantity.elements['id[2]'].focus(); return false; } if (((cart_quantity.elements['id[2]'].value == 9999) && (cart_quantity.elements['id[3]'].value == 99999))) alert('Please select an option!'); cart_quantity.elements['id[2]'].value = 9999; return false; } //--></script> I tried to assign id3 a value of 99999 so that on pages that it doesnt get reassigned after (i.e pages it does not exist on) it will still be equal to 99999 and pop up the error according to the second if clause. Unfortunately that did not work either. Is there any other way I can do via javascript? Hi, I know this is painfully obvious, but I can't get my head around it. Basically, these loops: Code: for (var p=0; p < routeInfo.length; p++) { times = routeInfo[p].getElementsByTagName("time"); dirs = routeInfo[p].getElementsByTagName("dirs"); dist = routeInfo[p].getElementsByTagName("dist"); for (q=0;q<dirs.length;q++) { count++; time = GXml.value(routeInfo[p].getElementsByTagName("time")[q]); dir = GXml.value(routeInfo[p].getElementsByTagName("dirs")[q]); dist = GXml.value(routeInfo[p].getElementsByTagName("dist")[q]); if (time.length>1) { way+='<div class="dir">'+count+'. '+dir+'</div>'+'<div class="results"><div class="time">'+time+'</div><div class="dist">'+dist+'</div><br><br>'; } else way+='<div class="dir">'+count+'. '+dir+'</div><br>'; } } outputs lines of text, with two <br> between them. What I want is for if it's the last loop for that only to be one <br> so I'm thinking something like this: Code: if (last loop) {end =<br>} else end=<br><br> ... way+='<div class="dir">'+count+'. '+dir+'</div>'+'<div class="results"><div class="time">'+time+'</div><div class="dist">'+dist+'</div>'+end; but I can't figure out what that "last loop" code should be. Any ideas? Hey Guys, I am a student doing my Cert IV in IT and am having a bit of trouble with some code. I have a form that when a letter is entered it returns a message saying you need to enter numbers in, and am trying to find a way of it checking if there are spaces in the numbers entered at the same time as checking if a number has been entered. My code looks as follows: Code: <html> <head> <title>Activity 2.25</title> </head> <script type="text/javascript"> function validateform() { var element; var BikeMoney; var TVMoney; var iPodMoney; var CarPrice; var flag; flag="OK" element=document.getElementsByTagName('input'); for(counter=0; counter<element.length; counter++) { switch (element[counter].type) { case "submit": break; default: if(isNaN(element[counter].value)) { alert("You will need to enter a number into " + element[counter].name); flag="NotOK" } else { BikeMoney=element[0].value; TVMoney=element[1].value; iPodMoney=element[2].value; CarPrice=element[3].value; } } } if(flag=="OK") if ((Number(BikeMoney)) && (Number(TVMoney)) && (Number(iPodMoney)) && (Number(CarPrice))) { TotalMoney = parseFloat (BikeMoney) + parseFloat (TVMoney) + parseFloat (iPodMoney) if(TotalMoney >= CarPrice) { alert("The total money is " + TotalMoney + " and the car price is " + CarPrice + " and you can afford the car"); } else { alert("The total money is " + TotalMoney + " and the car price is " + CarPrice + " and you cannot afford the car"); } } else { alert("Enter numbers Please"); } } </script> <body> <form name="input form" method="post" action=""> <table> <tr><td>Enter money from bike sale</td><td><input type="text" name="Bike Money"></td></tr> <tr><td>Enter money from TV sale</td><td><input type="text" name="TV Money"></td></tr> <tr><td>Enter money from iPod sale</td><td><input type="text" name="iPod Money"></td></tr> <tr><td>Enter the price of the car</td><td><input type="text" name="Car Price"></td></tr> <tr><td></td><td><input type="submit" value="Submit Details" onclick=validateform()></td></tr> </table> </form> </body> </html> Hello all, i'm looking for a way to check if the name of the page is equal to a link inside the page being checked. for instance: inside the address bar: http://www.mysite.test.html <body> <ul> <li><a href="test.html"></a></li> <li><a href="test1.html"></a></li> <li><a href="test2.html"></a></li> <li><a href="test3.html"></a></li> </body> please advise. thanks, jav _________
Hai Forum, May be this question is not in correct format, I want to know about how to check a particular file is exists or not ? using Javascript.One thing without using ActiveXObject because it is supports in IE only. Thanking You, Santosh Srinivas.G Hi all, Im in the process of creating a auction site for charity, im working on the last page which allows the user to enter a bid i have this javascript function that i have been working on to try and achieve the following. the user enters a bid use javascript to check to make sure its greater then and not equal to the current price simple right? But no its not well as im new to this maybe iv got lost in the code some where heres my code Code: function IsNewBidGreater(windowname) { var NewBid; var CurrentPrice; CurrentPrice = document.getElementById('MainContent_txtCurrentPrice').value; NewBid = document.getElementById('MainContent_txtNewBid').value; parseFloat(NewBid).toFixed(2); parseFloat(CurrentPrice).toFixed(2); if (NewBid < CurrentPrice || NewBid == CurrentPrice) { NewBid.className = "error"; document.getElementById('MainContent_ErrorLowBid').style.display = 'block'; return false; } else { document.getElementById('MainContent_ErrorLowBid').style.display = 'none'; document.getElementById('MainContent_BidConfirm').innerHTML = NewBid; ConPopUp(windowname) } } where i enter 10.50 and the current price is 9.99 i get the error message saying needs to be greater MainContent_ErrorLowBid is a hidden div on the web page which shows an error message any help would be highly appreciated Hi, im having a little difficulty checking if an XML node has a value, here the code: var Divs=new Array("artist","bio","img","date","tickets","venue","street","city","country","headliner"); xmlDoc=xmlhttp.responseXML; for ( nodes in Divs ) { if(!xmlDoc.getElementsByTagName(Divs[nodes])[0].childNodes[0].nodeValue) { } else { document.getElementById(Divs[nodes]).innerHTML= xmlDoc.getElementsByTagName(Divs[nodes])[0].childNodes[0].nodeValue+"<br>"; } } This always throws up the error: document.getElementById(Divs[nodes]) is null ive tried putting the xmlDoc in a variable then checking if its null but no luck, also tried the same method against "undefined" but no luck either. Would be greatful if anyone has any suggestions. Thanks, Tom. I am wanting all the inputs of the table cells turn red when invalid data (in this case anything other than a number) is entered. Object: 1. To add a className to those inputs 2. With the new className 'invalid" the CSS gives a red background to the input The appending of the className is not showing up in the generated source code and both Firefox and Chrome is not showing any errors in the code. I can't not figure out what is preventing the execution. Any advise to this simple problem will greatly be appreciated. Code: if (document.getElementsByTagName && document.getElementById) { var dg = { // references to the table table : document.getElementsByTagName('table')[0], tbody : document.getElementById('data').tBodies[0], //reference to the inputs dataInput : document.getElementsByTagName('input'), init : function() { // configure event listening and delegation this.util.configEvents(); // assign listeners to the table this.util.addEvent(this.table, 'keyup', this.checkData, true); }, checkData : function(dataInput) { // if it is not a number, mark the input as invalid if (isNaN(dataInput.value)) { dg.dataInput.className = 'invalid'; } // if the user deleted the value entirely, remove any invalid indication else if (dataInput.value === '') { dg.dataInput.className = ''; } }, util : { configEvents : function() { if (document.addEventListener) { this.addEvent = function(el, type, func, capture) { el.addEventListener(type, func, capture); }; this.stopBubble = function(evt) { evt.stopPropagation(); }; this.stopDefault = function(evt) { evt.preventDefault(); }; this.findTarget = function(evt, targetNode, container) { var currentNode = evt.target; while (currentNode && currentNode !== container) { if (currentNode.nodeName.toLowerCase() === targetNode) { return currentNode; break; } else { currentNode = currentNode.parentNode; } }; return false; }; } else if (document.attachEvent) { this.addEvent = function(el, type, func) { el["e" + type + func] = func; el[type + func] = function() { el["e" + type + func] (window.event); }; el.attachEvent("on" + type, el[type + func]); }; this.stopBubble = function(evt) { evt.cancelBubble = true; }; this.stopDefault = function(evt) { evt.returnValue = false; }; this.findTarget = function(evt, targetNode, container) { var currentNode = evt.srcElement; while (currentNode && currentNode !== container) { if (currentNode.nodeName.toLowerCase() === targetNode) { return currentNode; break; } else { currentNode = currentNode.parentNode; } }; return false; }; } } } }; dg.init(); } |