JavaScript Irish Mobile Regex - javascript

I'm trying to validate a form to check among other things that a user has added a valid Irish mobile which should start with 083,085,086,087,089 and be ten digits in length
I looked at Validate an Irish mobile phone number but I can't get it to work
My check function
function CheckPhone(pNumb) {
var paswd=/08[3679]-\d{0,10}$/;
return paswd.test(pNumb);}
Where I try to validate
if(!CheckPhone(d))
{
alert("You have not entered a valid Irish mobile number");
document.getElementById('phoneNumber').focus();
return false;
}
But even with a valid number I'm told that the entry is invalid. I'm sure I've done something wrong but I'm new to regex and not a programmer so not sure what.
In response to Jayce444 I've edited my question to include the full script as when I add the checkphone function it causes the email and password functions to be ignored and the form is submitted with invalid values present for these fields such as test#test and p2 for the password
<script>
function validateEmail(email) {
var re = /^(([^<>()[\]\\.,;:\s#\"]+(\.[^<>()[\]\\.,;:\s#\"]+)*)|(\".+\"))#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(email);
}
function CheckPassword(inputtxt) {
var paswd=/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,20}$/;
return paswd.test(inputtxt);
}
function CheckPhone(pNumb) {
var paswd=08[3679]-\d{7}$;
return paswd.test(pNumb);
}
function validateForm() {
var a=document.forms["subForm"]["firstName"].value;
if (a===null || a==="")
{
alert("All fields in the form are required You must enter your first name");
document.getElementById('firstName').focus();
return false;
}
var b=document.forms["subForm"]["lastName"].value;
if (b===null || b==="")
{
alert("All fields in the form are required You must enter your last name");
document.getElementById('lastName').focus();
return false;
}
var c=document.forms["subForm"]["email"].value;
if (c===null || c==="")
{
alert("All fields in the form are required You must enter your email address");
document.getElementById('email').focus();
return false;
}
if(validateEmail(c)===false)
{
alert("You must enter a valid email address");
document.getElementById('email').focus();
return false;
}
var d=document.forms["subForm"]["phoneNumber"].value;
if (d===null || d==="")
{
alert("All fields in the form are required You must enter your phone number");
document.getElementById('phoneNumber').focus();
return false;
}
if(CheckPhone(d)===false)
{
alert("You have not entered a valid Irish mobile number");
document.getElementById('phoneNumber').focus();
return false;
}
var e=document.forms["subForm"]["password"].value;
if (e===null || e==="")
{
alert("All fields in the form are required You must enter a password");
document.getElementById('password').focus();
return false;
}
if(CheckPassword(e)===false)
{
alert("You have not entered a valid password");
document.getElementById('password').focus();
return false;
}
}
</script>

This regex works:
08[3679]-\d{7}$
Btw this site: https://regex101.com/ is good to help test regexes and see what they're actually doing. The second part of yours, \d{0,10} says "match 0 to 10 digits". What you want is to match exactly 7 digits after the dash.

Related

Is there a way to fix the email and title validation in my CSS and JavaScript?

I am trying to validate my email and Title input field but it still aloows me to submit the page. Where is the error in the code please?
if(email == "") {
printError("emailErr", "Please enter your email address");
} else {
var regex = /^\S+#\S+\.\S+$/;
if(regex.test(email) === false) {
printError("emailErr", "Please enter a valid email address");
} else{
printError("emailErr", "");
emailErr = false;
}
}
if(title == "Select") {
printError("titleErr", "Please select your title");
} else {
printError("titleErr", "");
titleErr = false;
}
if((nameErr || emailErr || titleErr) == true) {
return false;
}
I expect it to provide an error message when the submit button is clicked. Link to my entire code https://jsfiddle.net/lizzyblue02/spmygzdw/
Overall it works fine, I just had to make a couple of changes.
Your input type="submit" submits the form every time it is clicked, even if the form validation returned errors. I changed it in input type="button", and the form is now submitted using JavaScript and only if the form validation returned no error.
As mentioned in the above comment, you also needed to close the validate function later in the function.
You can see result here.

Why Javascript validation is not working in html form?

When i try name box is fill with character it always show that "Name must be in character only.".
Here is Javascript code:
function validate_form() {
if (!(/^[A-Za-z]+$/).test(document.emp.new_name.value)) {
alert("Name must be in character only.");
return false;
}
if (!(/^\d{10}$/).test(document.emp.new_number.value)) {
alert("Enter valid mobile number");
return false;
}
if (!(/^[0-9.]+$/).test(document.emp.new_salary.value)) {
alert("salary must be numeric");
return false;
}
if (!(/^\w+([\.-]?\w+)*#\w+([\.-]?\w+)*(\.\w{2,3})+$/).test(document.emp.new_email.value)) {
alert("You have entered an invalid email address!")
return (false);
}
alert ("success");
return true;
}
Because initially it does not have any characters, try adding a check to character length
Checkout example
$("#submit").on("click",function(){
var name = $("#name").val();
if (name.length>0 && !(/^[A-Za-z]+$/).test(name)) {
alert("Name must be in character only.");
return false;
}
else{
alert("ok");
}
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="name"><input type="submit" id="submit">
var name = document.emp.new_name.value;
if (name.value.search(/[^a-zA-Z]+/) === -1) {
alert("Name must be in character only.");
return false;
}
Try with this code. Hopefully, this will work.

javascript validation numerical

Hi sorry i'm still pretty new to javascript.
I've developed a form in HTML and now i'm attempting to add javascript to validate the form.
So far i have simple javascript to make sure each element is filled in,
if (document.order.suburb.value=="")
{
alert("Suburb Cannot Be Empty")
return false
}
if (document.order.postcode.value=="")
{
alert("Postcode Cannot Be Empty")
return false
}
I then have javascript to validate the length of some of the elements,
if (document.order.telephone.value.length < 10)
{
alert("Invalid Telephone Number")
return false
}
Now i'm trying to validate numeric values in the telephone number part but it's not executing correctly, it's like the code is just ignored when it's being executed.
var digits="0123456789"
var temp
var i
for (i = 0 ; i <document.order.telephone.value.length; i++)
{
temp=document.order.telephone.value.substring(i,i+1)
if (digits.indexOf(temp)==-1)
{
alert("Invalid Telephone Number")
return false
}
}
Thanks for reading and thanks for the help :) been stuck on this issue for weeks and have no idea what i'm doing wrong, i tried to code on a separate document with another form and it seemed to work fine.
EDIT
Code for validation for digits in postcode
var post = document.order.postcode.value.replace(white,'');
if(!post){
alert("Post code required !");
return false;
}
post = post.replace(/[^0-9]/g,'');//replace all other than digits
if(!post || 4 > postcode.length) {
alert("Invalid Postcode !");
return false;
}
You may try this example:
var validate = function() {
var white = /\s+/g;//for handling white spaces
var nonDigit = /[^0-9]/g; //for non digits
if(!document.order.suburb.value.replace(white, '')) {
alert("Suburb required !");
return false; //don't allow to submit
}
var post = document.order.postcode.value.replace(white, '')
if(!post) {
alert("Post code required !");
return false;
}
post = post.replace(nonDigit,'');//replace all other than digits
if(!post || 6 > post.length) { //min post code length
alert("Invalid Post code !");
return false;
}
var tel = document.order.telephone.value.replace(white, '');
if(!tel) {
alert("Telephone required !");
return false;
}
tel = tel.replace(nonDigit,'');
if(!tel || 10 > tel.length) {
alert("Invalid Telephone !");
return false;
}
return true; //return true, when above validations have passed
};
<form onsubmit="return validate();" action="#" name="order">
Suburb: <input type="text" id="suburb" name="suburb" ><br/>
Post code: <input type="text" id="postcode" name="postcode"/><br/>
Telephone: <input type="text" id="telephone" name="telephone"/><br/>
<input type="reset"/><input type="submit"/>
</form>
Here is a FIDDLE that will give you something to think about.
You could handle this task in hundreds of ways. I've just used a regex and replaced all of the non-numbers with '' - and compared the length of two variables - if there is anything other than a number the length of the regex variable will be shorter than the unchanged mytelephone.
You can do all kinds of "validation" - just me very specific in how you define "valid".
JS
var mysuburb, mypostcode, mytelephone;
$('.clickme').on('click', function(){
mysuburb = $('.suburb').val();
mypostcode = $('.postcode').val();
mytelephone = $('.telephone').val();
console.log(mysuburb + '--' + mypostcode + '--' + mytelephone);
if(mysuburb.length < 1)
{
$('.errorcode').html('');
$('.errorcode').append('Suburb is required');
return false;
}
if(mypostcode.length < 1)
{
$('.errorcode').html('');
$('.errorcode').append('postode is required');
return false;
}
if( mytelephone.length < 1 )
{
$('.errorcode').html('');
$('.errorcode').append('telephone number is required');
return false;
}
if( mytelephone.length != mytelephone.replace(/[^0-9]/g, '').length)
{
$('.errorcode').html('');
$('.errorcode').append('telephone number must contain only numbers');
return false;
}
});

JS Numeric and Hyphen validation

I am using Javascript to validate phone number with hyphen. It is validating only one hyphen but not allowing two hyphens.
I want to validate this format:
123-456-7891
How can I do this with JS?
This is my function
function numericValidation(phoneno) {
var numbers = /^\d+((;\d+)*|-\d+)?$/;
if (phoneno.match(numbers)) {
alert('Your input is valid');
return true;
} else {
alert('Please enter in (123-456-7891) format');
return false;
}
}
function numericValidation(phoneno) {
var numbers =/^\d+(-\d+)*$/;
if (phoneno.match(numbers)) {
alert('Your input is valid');
return true;
}
else {
alert('Please enter in (123-456-7891) format');
return false;
}
}

JavaScript only working in Chrome

After field validation, the code below is not returning any values of the previous page. But this code is only working in Chrome. Firefox and IE are not working.
function quickenquiryValidator() {
// Make quick references to our fields
var regex = /^[a-zA-Z ]{2,30}$/;
var qename = document.getElementById('qename');
var qemobile = document.getElementById('qemobile');
var qeemail = document.getElementById('qeemail');
var calculation = document.getElementById('BotBootInput_Enquiry').value;
// Check each input in the order that it appears in the form!
//if(isAlphabet(qename, "Please enter only letters for your name")){
if (regex.test(qename.value)) {
if (isNumeric(qemobile, "Please enter a valid mobile number")) {
if (emailValidator(qeemail, "Please enter a valid email address")) {
if (check_calculation(calculation, "Please enter valid calculation result")) {
document.getElementById("submit4").setAttribute("type", "submit");
document.quickenqform.submit();
return true;
}
}
}
}
else {
alert("Please enter only letters for your name");
qename.focus();
return false;
}
return false;
}

Categories

Resources