Validate form input for domains and IPs - ajax - ajax

how to validate a form field that gets submitted through ajax (below). It needs to validate against a domain (.com, .net, .se etc.) and an IP address. Basically it has to look for at least one dot and at least two letters after last dot.
Now I only have an empty field validation:
var domain = $("input#domain").val();
if (domain == "") {
$("label#domain_error").show();
$("input#domain").focus();
return false;
}
Thank you!

You probably want to use regular expressions:
For the IP address:
^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$
For the hostname:
^(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$
For an example how to use regular expressions in JavaScript, have a look at this site

Related

Pattern attribute for <g:textField>

I'm already matching the patterns in my domain classes's constraints block, but now have been asked to add the pattern attribute to the <input> fields in the forms.
For some reason I am having lots of troubles doing this. The patters are working when I am checking them in w3schools or html5pattern, but not working in my <g:textField>. Here are the patterns i'm using:
First/Last names: "[A-Za-z-' .]{3,20}"
Street Address: [a-zA-Z0-9-#.,: ]
Email: [A-Za-z0-9._-]+#[a-z0-9.-]+\.[a-z]{2,3}$ (even after i'm using type="email" for this field, it's failing basic checks)
And a SEARCH field in a different form: "[^'\x22\x3A\x3C\x3D\x3E\x7B\x7D]+" ( ' or " or < or > or = or } or { are not allowed)
Any help will be highly appreciated!!

Recipient.Address return incorrect data

If I take user from contacts (in addon for outlook) and do this:
Outlook.Recipient recipient = c.GetMember(i);
AplicationLog(recipient.Address);
It will return this:
/0=:someData:/ou=Exchange Administrative Group (:someData:)/cn=Recipients/cn=:curentUserName:eed
It add "eed" to user name. And only for one.
Why it do that? How can I fix that?
P.S. I don't do anything with that data before it will be printed.
That looks like a perfectly valid EX (as opposed to SMTP) address.
If AddressEntry.Type is "SMTP", just use AddressEntry.Address. If Type == "EX", use AddressEntry.GetExchangeUser().PrimarySmtpAddress. AddressEntry object is returned by the Recipient.AddressEntry property.

!preg_match for email checking

I have this command to check for valid email address. I just found out that when I try to add this to our email server (all email requests off this form are local email addresses), the email server does not allow a numeric character to start the email address/username. I have read through all the documentation for the command preg_match and cannot find how to make it fail if it starts with a numeric in the first character location. I am a newbie so any help would be appreciated.
if (!preg_match("(^[-\w\.]+#([-a-z0-9]+\.)+[a-z]{2,4}$)i", $in_email))
In php you can use following code with php filter_var function which return a boolean after filtering the variable with a specified filter condition.
if(filter_var($email,FILTER_VALIDATE_EMAIL))
{
//valid email
}
else{
//INVALID EMAIL
}
The function filter_var will return true if email is in correct format otherwise false.
Try this one;
/^[^0-9][_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/
And use as follows
$regex = '/^[^0-9][_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/';
if (preg_match($regex, $email)) {
// Valid email
} else {
// Invalid email
}
If we have domains without dots this answers does not work. For this case I changed from:
/^[^0-9][_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/
To:
/^[^0-9][_a-z0-9-]+(\.[_a-z0-9-]+)*#([a-z0-9-]{2,})+(\.[a-z0-9-]{2,})*$/
Update: The user #Toto saw correctly one problem with regex that can start with any chars instead off numeric. And example like: #-----.--.---.----#-- was validate. So I changed /^[^0-9] for /^[a-z] and now is correct:
/^[a-z][_a-z0-9-]+(\.[_a-z0-9-]+)*#([a-z0-9-]{2,})+(\.[a-z0-9-]{2,})*$/
And work for these use cases:
user#domain
aa#aa
aa#aa.aa
aa.aa#aa

Validation of phone numbers containing '+' and '-' characters

In CodeIgniter, how do i validate phone numbers containing '+' and '-' symbols?
You cannot enter a number with "-" since you defined integer as the validation rule. Therefore, the validation will fail. You need to work with RegEx so that you can create more complex validation. See the topic on validations in the CI manual for more info.
Your validation rule:
$this->form_validation->set_rules('foo', 'Number', 'callback_number_validation');
Note how the keyword callback_ is used to identify CI your function for validation.
Your callback function:
//$str will be the value you want to verify
function number_validation($str) {
return preg_match("your_regex", $str) ? true: false;
}

Ruby email check (RFC 2822)

Does anyone know what the regular expression in Ruby is to verify an email address is in proper RFC 2822 email format?
What I want to do is:
string.match(RFC_2822_REGEX)
where "RFC_2822_REGEX" is the regular expression to verify if my string is in valid RFC 2882 form.
You can use the mail gem to parse any string according to RFC2822 like so:
def valid_email( value )
begin
return false if value == ''
parsed = Mail::Address.new( value )
return parsed.address == value && parsed.local != parsed.address
rescue Mail::Field::ParseError
return false
end
end
This checks if the email is provided, i.e. returns false for an empty address and also checks that the address contains a domain.
http://theshed.hezmatt.org/email-address-validator
Does regex validation based on RFC2822 rules (it's a monster of a regex, too), it can also check that the domain is valid in DNS (has MX or A records), and do a test delivery to validate that the MX for the domain will accept a message for the address given. These last two checks are optional.
Try this:

Resources