I have this mobile number format
+14354444744
It can accept upto 15 digits and + is mandatory
no other characters or special characters should be there
function phonenumber(inputtxt) {
var phoneno = /^([+]\d{2})?\d{15}$/;
if(inputtxt.value.match(phoneno)) {
return true;
}
else {
return false;
}
}
Any solution thanks.
function phonenumber(inputtxt) {
if(inputtxt[0]==="+") {
number=inputtxt.substring(1,inputtxt.lenght);
if(Number(number)){
if (number.length >= 15){
return true;
} else {
return false;
}
} else {
return false;
}
} else {
return false;
}
}
console.log(phonenumber("+1433f3333333"));
Related
function lessThanNinety(num) {
//return true if num is less than ninety
//otherwise return false
//code here
}
Here is the problem, here is my solution.
function lessThanNinety(num) {
if (number < 90) {
return true;
}
else {
return false;
}
}
var number = 50;
Where am I going wrong? Thanks
Seems like you are using wrong variable name, change variable number to num:
function lessThanNinety(num) {
if (num < 90) {
return true;
}
else {
return false;
}
}
and you will need to call the function:
var number = 50;
lessThanNinety(number);
I am unable to print a single output statement when there is no match. At the moment, if there is no match (no number incl. in password) it will print out multiple times until it does find a match.
Could anyone take a look at my dilemma below please:
var password = "password1";
var i = 0;
function checkPassword(password) {
if (password === "") {
console.log("password cannot be empty");
} else if (password.length < 8) {
console.log("password should be at least 7 characters");
} else {
while (i < password.length) {
if (password[i] == password.match(/[0-9]/g)) {
console.log("found: " + password[i]);
} else {
console.log("not found");
}
i++;
}
}
}
var password = "password1";
var i = 0;
function checkPassword(password) {
if (password === "") {
console.log("password cannot be empty");
} else if (password.length < 8) {
console.log("password should be at least 7 characters");
} else {
var found = false;
while (i < password.length) {
if (password[i].match(/[0-9]/g)) {
found = true;
break;
}
i++;
}
if(found) {
console.log("found");
} else {
console.log("not found");
}
}
}
But, if you are only looking for a number in the string, you should better do it like this:
var password = "password1";
function checkPassword(password) {
if (password === "") {
console.log("password cannot be empty");
} else if (password.length < 8) {
console.log("password should be at least 7 characters");
} else if(!password.match(/[0-9]{1,}/)) {
console.log("password should contain at least one number");
} else {
console.log("okay");
}
}
Here is a JSFiddle: https://jsfiddle.net/7btt1axb/
Something like this?
var password = "password1";
function checkPassword(password) {
if (password === "") {
console.log("password cannot be empty");
} else if (password.length < 8) {
console.log("password should be at least 7 characters");
} else {
var i = 0, found = false;
while (i < password.length && !found) {
if (password[i] == password.match(/[0-9]/g)) found = true;
else i++;
}
if (found) {
console.log("found: " + password[i]);
} else console.log("not found");
}
}
checkPassword(password);
I'm trying to get this code to work, but it's not working. I want to validate something with JavaScript
Code:
function validate() {
var letters = /^[A-Za-z]_$/;
u = document.getElementById("username").value;
if (validate.value.match(letters) {
return true;
} else {
alert("username is requiried");
return false;
}
}
return true;
}
Your code is not proper and also have lots of mistakes. I have corrected and implemented validations. Try the below code.
<script>
function validate() {
var u = document.getElementById("username").value;
var p = document.getElementById("password").value;
var letters = /^[A-Za-z]_$/;
if(u == '') {
alert("username is required");
return false;
}
if(!u.match(letters)) {
alert("username should be letters");
return false;
}
if(p == '') {
alert("password is required");
return false;
}
if(p.length < 6) {
alert("password length is too short");
return false;
}
return true;
}
</script>
Hope this will help
Try this code
var isEven = function(number) {
if (number % 2 === 0) {
return true;
} else if (isNaN(number)) {
return "Not a number";
} else {
return false;
}
}
isEven();
This must work for you. I got this one as correct !!!!
You get a ReferenceError because there is no isNan function. Its called isNaN.
var isEven = function(number) {
if (number % 2 === 0) {
return true;
} else if (NaN(number)) {
return "Your input is not a number!";
} else {
return false;
}
};
var isEven = function(number) {
// Your code goes here!
if(number%2){
return true;
}
else if(isNan (number)){
return "Your input is not a number.";
}
else{
return false;
}
};`enter code here`
Check it out:
var isEven = function(number) {
if (isNaN (number)) {
return "The input was not a number!";
}
else if (number % 2 === 0) {
return true;
}
else if (isNaN (number) === true) {
return "Your input isn't a number!";
}
else {
return false;
}
};
I have two two date fields - from date and to date, and i need to validate 3 things
Both the values are entered or not
Date datatype check
To date must be greater than from date.
But my script is not working.
can some body please check?
Thanks
function checkBothDates(sender,args)
{
var from = document.getElementById(sender.From);
var to = document.getElementById(sender.To);
var behaviorId = sender.behavior;
var from_value = from.value;
var to_value = to.value;
if((from_value == "")&&(to_value == ""))
{
args.IsValid = true;
}
else
{
if((from_value != "")&&(to_value != ""))
{
if((isValidDate(from_value))&&(isValidDate(to_value)))
{
if(from_value < to_value)
{
args.IsValid = false;
sender.errormessage = "To date must be greater than or equal to the from date";
}
}
else
{
args.IsValid = false;
sender.errormessage = "Please enter valid dates in both the fields";
if(behaviorId != null)
{
openCollapsiblePanel(behaviorId);
}
}
}
else
{
args.IsValid = false;
sender.errormessage = "Please make sure you enter both the values";
if(behaviorId != null)
{
openCollapsiblePanel(behaviorId);
}
}
}
}
function isValidDate(val)
{
var format = 'dd/MM/yyyy'
var regexp = /^\d{1,2}\/\d{1,2}\/\d{4}$/;
if (!regexp.test(val))
{
return false;
}
else
{
try
{
$.datepicker.parseDate(format,val,null);
return true;
}
catch(Error)
{
return false;
}
}
}
Your code is pretty repetitive, you can shorten a lot of it.
Also note that the regex check is entirely unnecessary, since $.datepicker.parseDate() won't accept anything invalid anyway.
function checkBothDates(sender, args) {
var from = parseDate( $(sender.From).val() ),
to = parseDate( $(sender.To).val() );
args.IsValid = false;
if (from == "" && to == "" || from && to && from <= to) {
args.IsValid = true;
} else if (from == null || to == null) {
sender.errormessage = "Please enter valid dates in both the fields";
} else if (from > to) {
sender.errormessage = "To date must be greater than or equal to the from date";
} else {
sender.errormessage = "Please make sure you enter both the values";
}
if (!args.IsValid && sender.behavior) {
openCollapsiblePanel(sender.behavior);
}
}
function parseDate(val) {
if (val == "") return "";
try {
return $.datepicker.parseDate('dd/MM/yyyy', val);
} catch (ex) {
return null;
}
}
There is a problem in your code aroun the 19th line. You wrote:
if(from_value < to_value) {
args.IsValid = false;
sender.errormessage = "To date must be greater than or equal to the from date";
}
But you definitely want that from_value is smaller then to_value. Fix it!