So I know what it does, because I found it online. It's a phone number validator. But that seemingly long list of seemingly random symbols in the first line of the function, I don't know where to look to write javascript in such shorthand. [javascript] function ValidatePhone() { var phoneRegExp = /^((\+)?[1-9]{1,2})?([-\s\.])?((\(\d{1,4}\))|\d{1,4})(([-\s\.])?[0-9]{1,12}){1,2}$/; var phoneVal = $("#txtPhone").val(); var numbers = phoneVal.split("").length; if (10 <= numbers && numbers <= 20 && phoneRegExp.test(phoneVal)) { alert("SUCCESS"); } } [/javascript]
That's about as short hand as it gets right there.. But that is a RegExpression that long list of code so continue your search there if you think you can do it differently. It makes sure the format is "XXX-XXX-XXXX"
It's simply a function that uses a regular expression to make the that the phone number is valid (or at least follows the correct amount of numbers as it should). Regular expressions (or regexs for short) are used very often in things like this or making sure emails are valid, etc. Some people come up with regex's themselves, but most people just google "phone number regex" and find what they are looking for as they are not easy to remember/create.
Regular expressions are extremely tedious to write. If you want some good education information AND some free expressions, go to RegExpLib.com You could create several validators just like the one you have and only need to update the expression part and function name. As to what that expression is actually doing, it's forcing the string to adhere to the rules within it. This site has great information on what each part of the regular expression string should do; http://www.regular-expressions.info/reference.html