473,396 Members | 2,052 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

package javascript

new javascript package to validate data

# hasValidChars(string, characters, caseSensitive)
# isIP(ip)
# isAlpha(string)
# isLetter(string)
# isNotEmpty(string)
# isIntegerInRange(number,min,max)
# isNum(number)
# isEmail(string)
# isZipCode(number,country)
# isDate(date,format)
# isMD5(string)
# isURL(string)
# isGuid(string)
# isISBN(string)
# isSSN(number)
# isDecimal(number)

Apr 24 '06 #1
5 3372
you can found it here :
http://www.mutationevent.com/projects/validate.js/

Apr 24 '06 #2
ac******@gmail.com wrote:
new javascript package to validate data
- uses prototype.js nonsense (unnecessarily, of course)
# hasValidChars(string, characters, caseSensitive)
| [...]
| hasValidChars:function(string, characters, caseSensitive){
| if(!string || !string.length) return false;
| if(!characters || !characters.length) return false;

One could argue that the empty string always meets the requirement.

| if(!caseSensitive){
| string = string.toLowerCase();
| characters = characters.toLowerCase();
| }
|
| var cArr = new Array();
| cArr = string.split("");
| var len = cArr.length;
| for(var i=0; i<len; i++){
| if(characters.indexOf(cArr[i]) == -1){
| return false;
| }
| }
| return true;
| }

- Unnecessarily inefficient and error-prone, in contrast to

function hasValidChars(s, characters, caseSensitive)
{
function escapeSpecials(s)
{
// quick hack
return s.replace(new RegExp("([\\\\-])", "g"), "\\$1");
}

return new RegExp(
"^[" + escapeSpecials(characters) + "]+$",
(!caseSensitive ? "i" : "")).test(s);
}
# isIP(ip)
| isIP:function (ip){
| if(!this.hasValidChars(ip,"0123456789.")){
| return false;
| }
|
| var parts = ip.split(".");
| if(parts.length!=4) return false;
| if (Number(parts[0]) == 0) {
| return false;
| }
| var len = parts.length;
| var stri;
| var numi;
| for (var i=0; i<len; i++) {

for (var i = len; i--;) {

| stri = parts[i];
| numi = parseInt(stri);

The base is missing.

| if (numi > 255 || stri != numi.toString()) {
| return false;
| }
| }
| return true;
| }

- IP is the Internet Protocol, not the Internet Protocol address.
- It does not recognize or misrecognizes invalid IPv4 addresses;
"0.017.377.0" is read as "0.15.255.0" and not considered invalid
by this method.
- It does not recognize IPv6 addresses.
# isAlpha(string)
| isAlpha:function(string){
| return this.hasValidChars(string,"0123456789abcdefghijklm nopqrstuvwxyz");
| }

- It ignores that alphabets are not restricted to the Latin alphabet, i.e.
it does not support Unicode alphabetic characters (although they are
well-defined and supported). Since about one fifth of this planet's
population are born Chinese (and the population is growing and markets
are emerging especially there), that is bordering to suicide, or rather
seppuku in that case ;-)

Besides, you were looking for

function isAlphaLatin(string)
{
return /^[0-9a-z]+$/i.test(string);
}
# isLetter(string)
| isLetter:function(string){
| },

(sic!)

I see.
# isNotEmpty(string)
| isNotEmpty:function(str,allowWhite){
| if(!allowWhite){
| return parseInt(str + "1") != 1 || parseInt(str) == 1;
| }
| return (str.length > 0);
| },

You only /post/ to cljs, right? We have discussed this shortly ago.
The current approach

function isEmpty(s)
{
return !/\S/.test(s);
}

can be modified easily to

function isNotEmpty(s)
{
return /\S/.test(s);
}

and then to

function isNotEmpty(s, bAllowWhitespace)
{
// inner parentheses only for better legibility
return (bAllowWhiteSpace
? (/\S/.test(s))
: (!/^$/.test(s)));
}
# isIntegerInRange(number,min,max)
| isIntegerInRange:function(n,Nmin,Nmax){
| var num = Number(n);

Undeclared identifier, becoming a global property. Bad style.

| if(isNaN(num)){
| return false;
| }
| if(num != Math.round(num)){
| return false;
| }
| return (num >= Nmin && num <= Nmax);
| },

- It does not allow for open intervals.
# isNum(number)
| isNum:function(number){
| numRegExp = /^[0-9]+$/

- Unnecessary undeclared identifier, becoming a global property.
Bad style at the worst.

| return numRegExp.test(number);
| }

So you know about Regular Expressions, after all? However:

- It does not handle non-integer values.
- It does not handle non-decimal values.
- It does not handle strings that can be considered not a number
(e.g. "000200").
# isEmail(string)
| isEmail:function(string){

- Correct would have been `isEMailAddr'.

| if(!string) return;
| if (string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)
| [A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1) return true;
| else return false;
| }

That's really a big laugh.

- Obviously you have never read STD11 or RFC2822.
- It does not handle IDNs.
# isZipCode(number,country)
| isZipCode:function(zipcode,country){
| if(!country) format = 'US';

- See above. (`country' or `format'?)

| switch(country){
| case'US': zpcRegExp = /^\d{5}$|^\d{5}-\d{4}$/; break;
| case'MA': zpcRegExp = /^\d{5}$/; break;
| case'CA': zpcRegExp = /^[A-Z]\d[A-Z] \d[A-Z]\d$/; break;
| case'DU': zpcRegExp = /^[1-9][0-9]{3}\s?[a-zA-Z]{2}$/; break;
| case'FR': zpcRegExp = /^\d{5}$/; break;
| case'Monaco':zpcRegExp = /^(MC-)\d{5}$/; break;
| }
| return zpcRegExp.test(zipcode);
| }

- It does not handle abbreviations of country names as prefixes but
for Monaco.
- Why you did not include support for ZIP codes of German-speaking
countries, the homelands of regular postal service in Europe,
remains a mystery.
# isDate(date,format)
| isDate:function(date,format){
| if(!format) format = 'FR';
| switch(format){
| case'FR': RegExpformat =
| /^(([0-2]\d|[3][0-1])\/([0]\d|[1][0-2])\/([2][0]|[1][9])\d{2})$/;
| break;
| case'US': RegExpformat =
| /^([2][0]|[1][9])\d{2}\-([0]\d|[1][0-2])\-([0-2]\d|[3][0-1])$/;
| break;
| case'SHORTFR': RegExpformat =
| /^([0-2]\d|[3][0-1])\/([0]\d|[1][0-2])\/\d{2}$/;
| break;
| case'SHORTUS': RegExpformat =
| /^\d{2}\-([0]\d|[1][0-2])\-([0-2]\d|[3][0-1])$/;
| break;
| case'dd MMM yyyy':RegExpformat =
| /^([0-2]\d|[3][0-1])\s(Jan(vier)?|Fév(rier)?|Mars|Avr(il)?|Mai|Juin
| |Juil(let)?|Aout|Septembre|Octobre|November|Decemb re)\s
| ([2][0]|[1][19])\d{2}$/; break;
| case'MMM dd, yyyy':RegExpformat =
| /^(J(anuary|u(ne|ly))|February|Ma(rch|y)|A(pril|ugu st)
| |(((Sept|Nov|Dec)em)|Octo)ber)\s([0-2]\d|[3][0-1])\,
| \s([2][0]|[1][9])\d{2}$/; break;
| }
| return RegExpformat.test(date);
| }

I can hardly wait for Dr Stockton's comment on this.
# isMD5(string)
| isMD5:function(string){
| if(!string) return false;
| md5RegExp = /^[a-f0-9]{32}$/;

- See above.

| return md5RegExp.test(string);
| }

I don't see the point, i.e. I doubt the usefulness of this method.
# isURL(string)
| isURL:function(string){
| string = string.toLowerCase();
| urlRegExp = /^http\:\/\/|https:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3
^^^^^^^^^ ^^^^^^^^^^^^^^^^^
- See above.

| (\/\S*)?$/
| return urlRegExp.test(string);
| }

- Obviously you have never read RFC3986.
- Because of the unparathesed alternation, it does not handle
correct "http:"-scheme URLs.
# isGuid(string)
| isGuid:function(guid){
| //guid format : xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx or
| xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
| GuidRegExp = /^[{|\(]?[0-9a-fA-F]{8}[-]?([0-9a-fA-F]{4}[-]?){3}
| [0-9a-fA-F]{12}[\)|}]?$/

- See above.

| return GuidRegExp.test(guid);
| },
# isISBN(string)
| isISBN:function(number){
| ISBNRegExp =
| /ISBN\x20(?=.{13}$)\d{1,5}([- ])\d{1,7}\1\d{1,6}\1(\d|X)$/

- See above.

| return ISBNRegExp.test(number);
| },
# isSSN(number)
| isSSN:function(number){
| //Social Security Number format : NNN-NN-NNNN
| ssnRegExp = /^\d{3}-\d{2}-\d{4}$/
| return ssnRegExp.test(number);
| }

Well, my valid SSN does not match.
# isDecimal(number)


| isDecimal:function(number){
| // positive or negative decimal
| decimalRegExp = /^-?\d*(\.\d+)?$/
| return decimalRegExp.test(number);
| }

- "-.0" really is definitely not a decimal number. Trust me.

And I really wonder why you did not use RegExp all the way, but insisted on
more inefficient approaches. This strikes me as just being copypasted from
others (and "forgotten" to mention the source) without having a minimum
clue yourself.

- Providing _only_ a RAR-compressed file on the Web is bordering to suicide.

That are enough minuses for me not only not to recommend it, but to
recommend against it.

Thanks for a long big laugh anyway.
PointedEars
--
#define QUESTION ((bb) || !(bb))
// William Shakespeare (if he would have been a hacker ;-))
Apr 24 '06 #3
JRS: In article <11**********************@i39g2000cwa.googlegroups .com>
, dated Mon, 24 Apr 2006 06:49:54 remote, seen in
news:comp.lang.javascript, ac******@gmail.com posted :
new javascript package to validate data

# hasValidChars(string, characters, caseSensitive)
# isIP(ip)
# isAlpha(string)
# isLetter(string)
# isNotEmpty(string)
# isIntegerInRange(number,min,max)
# isNum(number)
# isEmail(string)
# isZipCode(number,country)
# isDate(date,format)
# isMD5(string)
# isURL(string)
# isGuid(string)
# isISBN(string)
# isSSN(number)
# isDecimal(number)


Post it here, in sections of reasonable size, and it will be publicly
evaluated. Or does that thought frighten you?

How about starting with isLetter?

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Apr 25 '06 #4
"Thomas 'PointedEars' Lahn" <Po*********@web.de> wrote in message
news:15****************@PointedEars.de...

[snip]
- It ignores that alphabets are not restricted to the Latin alphabet, i.e.
it does not support Unicode alphabetic characters (although they are
well-defined and supported). Since about one fifth of this planet's
population are born Chinese (and the population is growing and markets
are emerging especially there), that is bordering to suicide, or rather
seppuku in that case ;-)


[snip]

Seppuku is ritual Japanese suicide, not Chinese. :)
Apr 25 '06 #5
McKirahan wrote:
"Thomas 'PointedEars' Lahn" [...] wrote [...]
- It ignores that alphabets are not restricted to the Latin alphabet,
i.e. it does not support Unicode alphabetic characters (although they
are well-defined and supported). Since about one fifth of this
planet's population are born Chinese (and the population is growing
and markets are emerging especially there), that is bordering to
suicide, or rather seppuku in that case ;-)


[snip]

Seppuku is ritual Japanese suicide, not Chinese. :)


Yes, I knew, but the joke wouldn't have worked then :) (Hence "rather".)
F'up2 poster

PointedEars
--
Germany is the birth place of psychiatry. Psychs feel threatened by
Scientology as they are crazy. Many psychs become psychs to heal their own
mental problems and to control others. -- "The only real Barbara Schwarz",
dsw.scientology, <16**************************@posting.google.com >
Apr 25 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Ronan | last post by:
Hi, I have a problem with a form using the PHP PEAR HTML_QuickForm package & javascript: I want to record the content of my form into a mySQL database then execute a javascript function. ...
6
by: Dan Webb | last post by:
Hi All, Im currently working on ways of pacakaging javascript functions/variables/objects in a similar way to the Java package statement so that scripts can be interact with each other with the...
4
by: Roger Redford | last post by:
Dear Experts, I'm attempting to marry a system to an Oracle 817 datbase. Oracle is my specialty, the back end mainly, so I don't know much about java or javascript. The system uses javascript...
3
by: Petterson Mikael | last post by:
Hi, I have the following package names ( in an xml) that I will transform to html. I need to sort them. <package name="se.company.product.subproduct.boam.fpx.testsignals"> <package...
136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
6
by: Ajit Goel | last post by:
Hi; We have a Javascript function which I have been tasked to move to a CSharp class. This javascript function uses Regular expression extensively. This function has a string input parameter and...
4
by: narsibvl | last post by:
Here is a piece of html code <form name="package_information" id="package_information" method="post" enctype="multipart/form-data""& lt;" <tr> <td><input type="checkbox"...
84
by: Patient Guy | last post by:
Which is the better approach in working with Javascript? 1. Server side processing: Web server gets form input, runs it into the Javascript module, and PHP collects the output for document prep....
2
by: ManidipSengupta | last post by:
Hi, a few (3) questions for the Java experts, and let me know if this is the right forum. It deals with 100% java code (reason for posting here) but manages a Web browser with Javascript. Thanks in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.