Sunday 15 May 2011

How do I check if there is a certain number of characters in an answer with JavaScript Form Validation -


I have this website where you can upload some information and it will be archived. I currently have this confirmation, but I am trying to make it more precise. I want to be able to send a warning ... if they have more than three. (Dots) answer there. Here's what I got:

  function ValidateContactForm () {var name = document.ContactForm.sName; Var ip = document.ContactForm.sIp; Var port = document.ContactForm.sPort; Var type = document.ContactForm.sType; Var Description = document.ContactForm.sDesc; If (name.value == "") {window.alert ("Please enter a server name"); Name.focus (); return false; } If (ip.value == "") {window.alert ("Please enter a valid IP address"); Ip.focus (); return false; } And if (ip.value.length> 18) {window.alert ("Please enter a valid IP address"); Ip.focus (); return false; } If (port.value == "" || port> 4 || port & lt; 6) {window.alert ("Please enter a valid port number"); Port.focus (); return false; } If (description.value == "") {window.alert ("Please enter a description"); Description.focus (); return false; } Back true; }   

I want to do this so that there is more than 3 points in the IP, it will alert you how will I do this? Thanks

Using RegExp, we can match all the points, and calculate matches Are there.

  if ((ip.value.match (/. / G) || []) Length! == 3) {window.alert ("Please enter a valid IP address") ; Ip.focus (); return false; }    

No comments:

Post a Comment