Filtering/Sanitizing User Input. Best Approach - cocoa

To clarify, I want to check for valid characters. For first name, last name I want to check for [A-Za-z]. For Email I want to check for chars valid for email.
What algorithm would I use to CHECK user input? Specifically, I'd like to CHECK the user's first name, last name and email address for valid characters before I add to database.
Obviously, something like this isn't sufficient:
if ( [firstName.text isEqualToString:#""] || [lastName.text isEqualToString:#""] || [emailAddress.text isEqualToString:#""]) { ... }

Revised for your edit:
What you probably want to look at is NSCharacterSet. It lets you define explicit sets of characters, which you can then use to test strings for presence of those characters. A trivial example:
NSCharacterSet* nonAlphaNumericSet = [[NSCharacterSet alphanumericCharacterSet] invertedSet];
if ([someString rangeOfCharacterFromSet:nonAlphaNumericSet].location != NSNotFound)
{
NSLog(#"It's not alphanumeric!");
}

Related

Check for only one specific special character in string

I want to check if a string contains only '*' and treat replace it with blank.
For eg:
Scenario 1: if the string contains '**abc*%#' or 'xyz*$#*!' then I need to retain their values as is.
Scenario 2: if the string contains values like '****' or '*' or any combination where only this special character is present in the string then I need an output of ''.
Please suggest what would be the best way to go about this.
Thanks.
You can achieve this by getting the length of the string and replacing the * with '' and then subtract both of them. If the length is 0 then it contains all *
Example
int count = line.length() - line.replace(".", "").length();
Hope this helps.
-Help :)

Processing-NullPointerException when converting string to char

So I have a set of different strings made if a certain key is pressed using keyPressed(), but some of those strings i want to convert to characters and tried doing so like this:
char keyChar = keyChanged.charAt(0);
except now i get a nullpointerexpression. if it matters, keyChanged would be a 1 letter string like "r".
You can avoid a NullPointerException like this:
if (keyChanged != null){
char keyChar = keyChanged.charAt(0);
}
Solved it, It was pretty stupid on my part, I just set the original keyChanged variable as a character instead.

Proper validation with [NSScanner: scanInteger] in Cocoa

I am converting a string to signed integer via NSScanner:scanInteger, but it seems to be accepting values such as '123abc' as '123', instead of throwing a error on invalid input.
I can do my own custom validation, but would prefer to find an API which will do the conversion and fail on '123abc'.
By the way, 'abc123' does fail with scanInteger, which is good.
I don't think that using a scanner is the way to do this -- you could, but there are easier ways. I would use the NSString method rangeOfCharactersFromSet: to check for non-digit characters.
NSCharacterSet *notDigits = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
NSUInteger nonDigits = [enteredString rangeOfCharacterFromSet:notDigits].length;
If nonDigits is not zero, then the user has entered something other than a digit. If you want to allow decimal points then you would have to create your own set that contains everything other than digits and the decimal point.

jquery/javascript - check if input (password) has some duplicated characters

I am working with passwords now and want to make user to create complicated password. I use jQuery to check if password is long enough and how strong it is depending on used characters. Still, cant figure out how to make user to create password with no duplicated chars, so he cant make password like '111111111111111111111111' or '1qAz1qAz1qAz' etc.
My code is here:
$("#new_pass").keyup(function() {
var newpass = $(this).val();
var chars = 0;
if((/[a-z]/).test(newpass)) chars += 26;
if((/[A-Z]/).test(newpass)) chars += 26;
if((/[0-9]/).test(newpass)) chars += 10;
if((/[^a-zA-Z0-9]/).test(newpass)) chars += 32;
var strength = Math.pow(chars, newpass.length)
alert(strength)
}
Thanks in advance.
Perhaps you could use something simple like this:
/(\w+)(?=\1)/g
It matches one or more word characters then checks to see if they are followed by that exact string.
Basic test (in Coffeescript)
pattern = /(\w+)(?=\1)/g
console.log pattern.test("1qAz1qAz1qAz")
console.log pattern.test("111111111111111")
console.log pattern.test("abcdefg")
console.log pattern.test("abccdefg")
Outputs:
true
true
false
true
As a side note, upper and lower case does matter, so for instance:
pattern.test("abcCdefg")
Would in fact return false.

How to correctly replace ereg with preg

I have a list of Mobile devices that I'm using to display content correctly. The depreciated function looks like this:
function detectPDA($query){
$browserAgent = $_SERVER['HTTP_USER_AGENT'];
$userAgents = $this->getBrowserAgentsToDetect(); // comma separated list of devices
foreach ( $userAgents as $userAgent ) {
if(eregi($userAgent,$browserAgent)){
if(eregi("iphone",$browserAgent) || eregi("ipod",$browserAgent) ){
$this->iphone = true;
}else{
$this->pda = true;
}
}
}
}
What is the correct way to replace the eregi functions?
If all the pattern strings ($userAgent and iphone) can be trusted not to contain special regex chars (()[]!|.^${}?*+), then you just surround the eregi regex with slashes (/) and add an i after the last slash (which means "case insensitive").
So:
eregi($userAgent,$browserAgent) --> preg_match("/$userAgent/i",$browserAgent)
eregi("iphone",$browserAgent) --> preg_match('/iphone/i',$browserAgent)
However, are you just trying to match $userAgent as-is within $browserAgent? For example, if a particular $userAgent was foo.bar, would you want the . to match a literal period, or would you want to interpret it in its regex sense ("match any character")?
If the former, I'd suggest you forgo regex entirely and use stripos($haystack,$needle), which searches for the string $needle in $haystack (case-insensitive). Then you don't need to worry about (say) an asterisk in $userAgent being interpreted in the regex sense instead of the literal sense.
If you do use stripos don't forget it can return a 0 which would evaluate to false, so you need to use === false or !== false (see the documentation I linked).

Resources