dflow Posted February 10, 2012 Share Posted February 10, 2012 I need to validated the following Pattern Name , Name , Name that's Uppercase or Lowercase space comma space i have the following: <script> function searchfieldvalidate(value) { return /^[a-z][A-Z]\W\,]+$^[a-z][A-Z]\W\,]+$^[a-z][A-Z]\W\,]+$/.test(value); } </script> Quote Link to comment https://forums.phpfreaks.com/topic/256830-validation-help/ Share on other sites More sharing options...
AyKay47 Posted February 10, 2012 Share Posted February 10, 2012 i must ask first, why are you using javascript to validate anything Quote Link to comment https://forums.phpfreaks.com/topic/256830-validation-help/#findComment-1316666 Share on other sites More sharing options...
joe92 Posted February 10, 2012 Share Posted February 10, 2012 That looks like JavaScript, is it? If it is, try: <script> function searchfieldvalidate(value) { if(value.match(/^[a-z]+\s,\s[a-z]+\s,\s[a-z]+$/i)) //i turns case insensitivity on, allowing upper and lower case values of what ever is inside the match { return 'Valid'; } else{ return 'Invalid'; } } </script> What you had would fail on many places. The first being the multiple use of caret and dollar, which out of a character class, [], mean match at the beginning and end of the string. There can't be three beginnings and ends. Secondly, \W (the opposite of \w) means match any character that isn't a word character, meaning it would allow characters like {'#}@&*() through. The third fail was you use of character classes, [a-z][A-Z]\W\,]+. Note in particular, ]+, at the end. That end close square bracket has no opening counterpart bracket so what are we repeating? The square bracket? I don't even know how that would be interpreted to be honest with you, but I can definitely tell you it's wrong. I could go on but I'd rather suggest you read this: http://www.regular-expressions.info/tutorial.html Start at the introduction and end after free spacing. You will go in a matter of days from a complete novice in regular expressions to someone who can handle himself with them quite confidently. It is a very good tutorial. Hope that helps you, Joe Quote Link to comment https://forums.phpfreaks.com/topic/256830-validation-help/#findComment-1316668 Share on other sites More sharing options...
dflow Posted February 10, 2012 Author Share Posted February 10, 2012 i must ask first, why are you using javascript to validate anything it's a client side on execution alert, not a POST validation Quote Link to comment https://forums.phpfreaks.com/topic/256830-validation-help/#findComment-1316726 Share on other sites More sharing options...
dflow Posted February 10, 2012 Author Share Posted February 10, 2012 That looks like JavaScript, is it? If it is, try: <script> function searchfieldvalidate(value) { if(value.match(/^[a-z]+\s,\s[a-z]+\s,\s[a-z]+$/i)) //i turns case insensitivity on, allowing upper and lower case values of what ever is inside the match { return 'Valid'; } else{ return 'Invalid'; } } </script> What you had would fail on many places. The first being the multiple use of caret and dollar, which out of a character class, [], mean match at the beginning and end of the string. There can't be three beginnings and ends. Secondly, \W (the opposite of \w) means match any character that isn't a word character, meaning it would allow characters like {'#}@&*() through. The third fail was you use of character classes, [a-z][A-Z]\W\,]+. Note in particular, ]+, at the end. That end close square bracket has no opening counterpart bracket so what are we repeating? The square bracket? I don't even know how that would be interpreted to be honest with you, but I can definitely tell you it's wrong. I could go on but I'd rather suggest you read this: http://www.regular-expressions.info/tutorial.html Start at the introduction and end after free spacing. You will go in a matter of days from a complete novice in regular expressions to someone who can handle himself with them quite confidently. It is a very good tutorial. Hope that helps you, Joe That looks like JavaScript, is it? If it is, try: <script> function searchfieldvalidate(value) { if(value.match(/^[a-z]+\s,\s[a-z]+\s,\s[a-z]+$/i)) //i turns case insensitivity on, allowing upper and lower case values of what ever is inside the match { return 'Valid'; } else{ return 'Invalid'; } } </script> What you had would fail on many places. The first being the multiple use of caret and dollar, which out of a character class, [], mean match at the beginning and end of the string. There can't be three beginnings and ends. Secondly, \W (the opposite of \w) means match any character that isn't a word character, meaning it would allow characters like {'#}@&*() through. The third fail was you use of character classes, [a-z][A-Z]\W\,]+. Note in particular, ]+, at the end. That end close square bracket has no opening counterpart bracket so what are we repeating? The square bracket? I don't even know how that would be interpreted to be honest with you, but I can definitely tell you it's wrong. I could go on but I'd rather suggest you read this: http://www.regular-expressions.info/tutorial.html Start at the introduction and end after free spacing. You will go in a matter of days from a complete novice in regular expressions to someone who can handle himself with them quite confidently. It is a very good tutorial. Hope that helps you, Joe for reason this accepts all now Quote Link to comment https://forums.phpfreaks.com/topic/256830-validation-help/#findComment-1316733 Share on other sites More sharing options...
AyKay47 Posted February 10, 2012 Share Posted February 10, 2012 That looks like JavaScript, is it? If it is, try: <script> function searchfieldvalidate(value) { if(value.match(/^[a-z]+\s,\s[a-z]+\s,\s[a-z]+$/i)) //i turns case insensitivity on, allowing upper and lower case values of what ever is inside the match { return 'Valid'; } else{ return 'Invalid'; } } </script> What you had would fail on many places. The first being the multiple use of caret and dollar, which out of a character class, [], mean match at the beginning and end of the string. There can't be three beginnings and ends. Secondly, \W (the opposite of \w) means match any character that isn't a word character, meaning it would allow characters like {'#}@&*() through. The third fail was you use of character classes, [a-z][A-Z]\W\,]+. Note in particular, ]+, at the end. That end close square bracket has no opening counterpart bracket so what are we repeating? The square bracket? I don't even know how that would be interpreted to be honest with you, but I can definitely tell you it's wrong. I could go on but I'd rather suggest you read this: http://www.regular-expressions.info/tutorial.html Start at the introduction and end after free spacing. You will go in a matter of days from a complete novice in regular expressions to someone who can handle himself with them quite confidently. It is a very good tutorial. Hope that helps you, Joe That looks like JavaScript, is it? If it is, try: <script> function searchfieldvalidate(value) { if(value.match(/^[a-z]+\s,\s[a-z]+\s,\s[a-z]+$/i)) //i turns case insensitivity on, allowing upper and lower case values of what ever is inside the match { return 'Valid'; } else{ return 'Invalid'; } } </script> What you had would fail on many places. The first being the multiple use of caret and dollar, which out of a character class, [], mean match at the beginning and end of the string. There can't be three beginnings and ends. Secondly, \W (the opposite of \w) means match any character that isn't a word character, meaning it would allow characters like {'#}@&*() through. The third fail was you use of character classes, [a-z][A-Z]\W\,]+. Note in particular, ]+, at the end. That end close square bracket has no opening counterpart bracket so what are we repeating? The square bracket? I don't even know how that would be interpreted to be honest with you, but I can definitely tell you it's wrong. I could go on but I'd rather suggest you read this: http://www.regular-expressions.info/tutorial.html Start at the introduction and end after free spacing. You will go in a matter of days from a complete novice in regular expressions to someone who can handle himself with them quite confidently. It is a very good tutorial. Hope that helps you, Joe for reason this accepts all now what? anyway, here is something i put together, since i am not a fan of using regex's that have to backtrack if it can be avoided (and im bored). <script type="text/javascript"> function validateStr(arr) { for(index=0;index<arr.length;index++) { if(/^[a-z]+$/i.test(arr[index]) === false) { return false; } } } var str="test , test , test"; var arr = str.split(" , "); if(validateStr(arr) === false) { alert("failed"); } else { alert("pass"); } </script> this splits the string by the delimiter space comma space, which will isolate each case, then checks against the regex, returning false if it fails. Quote Link to comment https://forums.phpfreaks.com/topic/256830-validation-help/#findComment-1316819 Share on other sites More sharing options...
dflow Posted February 10, 2012 Author Share Posted February 10, 2012 thanks i treid to implement it but nada <script> function validateStr(arr) { for(index=0;index<arr.length;index++) { if(/^[a-z]+$/i.searchform(arr[index]) === false) { alert("choose from list") return false; } } } var str = document.searchform.searchField.value var arr = str.split(" , "); </script> im trying to get the chosen result of an auto-complete to be validated Quote Link to comment https://forums.phpfreaks.com/topic/256830-validation-help/#findComment-1316840 Share on other sites More sharing options...
joe92 Posted February 11, 2012 Share Posted February 11, 2012 AyKay47, neat solution. However, I would like to pick you up on one point that you mentioned: anyway, here is something i put together, since i am not a fan of using regex's that have to backtrack if it can be avoided (and im bored). My solution has no backtracking in it... for reason this accepts all now Sorry, what? thanks i treid to implement it but nada <script> function validateStr(arr) { for(index=0;index<arr.length;index++) { if(/^[a-z]+$/i.searchform(arr[index]) === false) { alert("choose from list") return false; } } } var str = document.searchform.searchField.value var arr = str.split(" , "); </script> im trying to get the chosen result of an auto-complete to be validated And again, what? Can you please give a bit more information with your posts. What error's are being returned? Have you checked that you are passing the right information to the function? Which alert is popping up from AyKay's response? Your asking people to blind code a solution to a problem that you can't put into words. It's quite difficult. Quote Link to comment https://forums.phpfreaks.com/topic/256830-validation-help/#findComment-1316854 Share on other sites More sharing options...
abareplace Posted February 11, 2012 Share Posted February 11, 2012 If the spaces around commas are optional, then Joe's pattern should be modified in this way: /^[a-z]+\s*,\s*[a-z]+\s*,\s*[a-z]+$/i Quote Link to comment https://forums.phpfreaks.com/topic/256830-validation-help/#findComment-1316858 Share on other sites More sharing options...
AyKay47 Posted February 11, 2012 Share Posted February 11, 2012 AyKay47, neat solution. However, I would like to pick you up on one point that you mentioned: anyway, here is something i put together, since i am not a fan of using regex's that have to backtrack if it can be avoided (and im bored). My solution has no backtracking in it... ummm.... yeah I just looked it over again and it shouldn't backtrack on a match you're right. thanks i treid to implement it but nada <script> function validateStr(arr) { for(index=0;index<arr.length;index++) { if(/^[a-z]+$/i.searchform(arr[index]) === false) { alert("choose from list") return false; } } } var str = document.searchform.searchField.value var arr = str.split(" , "); </script> im trying to get the chosen result of an auto-complete to be validated wait... you edited the function and didn't call it in your code at all.. since you clearly do not understand how it should be implemented, i will hold you hand: <script> function validateStr(arr) //don't touch this function { for(index=0;index<arr.length;index++) { if(/^[a-z]+$/i.test(arr[index]) === false) { return false; } } } var str = document.searchform.searchField.value; //validate that this is in the correct format to be split var arr = str.split(" , "); //delimiter can be changed if desired if(validateStr(arr) === false) alert("failed"); else alert("passed"); </script> Quote Link to comment https://forums.phpfreaks.com/topic/256830-validation-help/#findComment-1316913 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.