I cant manage to put my javascript function to work [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 months ago.
Improve this question
Im creating accounts on a api and the password needs those restrictions.The handleRegist() is connecting to the api but the passwords i use always goes through that if cycle.I am using a function to check the password strength for a login the password must contain at least 1 special character 1 uppercase letter and it must be longer than 8 characters.
I have this code so far:
function passwordChecker(password){
var strongPassword = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z0-9])(?!.*\s).{8,15}$/;
if (password.value.match(strongPassword)) {
console.log("Good Password");
handleRegist();
}else
console.log("Try Again!\nnot strong enough");
}
NOTE: the strongPassword variable i found it on the internet and i am not sure if the restriction works like that.

To be able to fire the function you need to call it from somewhere, naturally, you would like to call it inside the onchange event inside the input component.
<input
type="text"
onChange={(e) => passwordChecker({ value: e.target.value })}
/>
Here is a working codesandbox example:
Working example
(open the console from the bottom of the page to see the result while typing inside the input)
Notice: im passing the value inside an object {value: e.target.value}, im doing it because inside your function you are checking password.value.match(strongPassword), it expect to get password.value, therefore i needed to add the value inside an object and pass it to the function.
i recommend to handle the value and store it in a state with useState and pass the state as value to the input.

Related

How do I make it show up how many questions the user gets right? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am trying to make a simple Algebra quiz in HTML and JavaScript, but can't figure out how to make it show up how many answers you got correct. I made it to where the it would turn the correct answer variables into booleans, true is correct and false is incorrect. Then I made if and else statements to see how many answers were correct but the alert (which will be changed) is just displaying zero no matter what. Here is a link to my code.
https://jsbin.com/yivapi/edit?,js
Trenner, I'm not going to be able to answer your question properly without untangling your code, so what I thought I'd do is show you a different (and hopefully easier) way to approach the problem. I hope that's okay.
HTML
Here are the inputs for the questions as I imagine you've set them out. I've only used three here for simplicity. There's a submit button which I'll get to in a moment.
<input id="q1" />
<input id="q2" />
<input id="q3" />
<button id="submit">Submit</button>
JavaScript
First we grab our submit button and assign a function getAnswers to its click event.
var submit = document.getElementById('submit');
submit.onclick = getAnswers;
This is a shortcut function to grab the input values so you don't need to keep writing document.getElementById(id).value later.
function getValue(id) {
return document.getElementById(id).value;
}
OK. So now, instead of having a load of if...else statements we're going to use an object to store all of our information. If you've used other languages they might call something like this a hash map. Basically it's a store of information that has key/value pairs and it's great for storing connected information together. In this case each question is a key which has another object as its value, and that object's keys are value and answer, and it's values are the value of the input element and the actual answer.
function getAnswers() {
// create the object
var quiz = {};
// for each question assign set up a new key (question1)
// and get the input value using our shortcut function.
// We set the actual answer here too.
quiz.question1 = { value: getValue('q1'), answer: '2√19' }
quiz.question2 = { value: getValue('q2'), answer: '√87' }
quiz.question3 = { value: getValue('q3'), answer: '8x√2' }
// Once we've loaded the questions, we can check the answers
// We pass in our quiz object as an argument
checkAnswers(quiz);
}
Here we use a loop to iterate over our quiz object. A simple loop of 7 lines instead of all those horrible if...else statements.
function checkAnswers(quiz) {
// set up out correctAnswers variable
var correctAnswers = 0;
// loop over the quiz object and...
for (var q in quiz) {
// ...for each question we test the input value we've stored
// against the answer also held in that object
// If they match increase the correctAnswers variable
if (quiz[q].value === quiz[q].answer) {
correctAnswers++;
}
}
// finally alert the correctAnswers
alert("You got " + correctAnswers + " out of 15 questions right!");
}
This DEMO will show you the working code. Just add 2√19 into the first input box and press submit and you will get an alert of 1 correct answer(s).
Sorry for not directly answering your question, but I hope you find this code interesting and useful. Good luck with your JavaScript coding.

JQuery Selector With Some Conditions [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
JQuery selector with multiple conditions Email Validation
hi i have to validate Email address.
my following code works fine:
contact_email = $('#contact_email').val();
var reg = /^([A-Za-z0-9_\-\.])+\#([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
//if Email address is empty it allowed but if it have any value then check whether proper email address or not
if(contact_email && !reg.test(contact_email)) {
err += 1;
$('#contact_email').addClass('boxerror');
}
it works but following code not work
contact_email = $('#contact_email').val().is(reg.test(this.val())).addClass('boxerror');
1.what mistake i had done?
2.JQuery with selector with multiple condition is possible
You are misunderstanding how .is(...) works. You have to pass a jquery selector in is() function, or you can also pass a function which will be called by jquery for all the function. And it will return a boolean value.
To accomplish your task you will be needed to use filter(...).
Here is the code to help you :
$("#contact_email").filter(function(index, element) {
return !reg.test(element.value);
}).addClass("boxerror");
Full documentation for filter() is here
Hence :
Mistake: you were using is() in wrong situation.
selector with multiple condition is possible if implemented jquery nicely.

getting default data in ng-model input [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
HTML
<div>
<input ng-model="tripsheet.tripsheet_num" type="text">
</div>
JS
$scope.getTotalTripsheets = function()
{
return $scope.tripsheets.length;
};
i want the above data(getTotalTripsheets) to to default input in the input tag, in the above code i am fetching the length from the database table and tried making it as the default data in the input field.
i tried somthing like this(below), it dos not work, how should i proceed with this?
$scope.tripsheet = function()
{
$scope.tripsheet_num = !$scope.getTotalTripsheets;
};
If you just want a one-time/one-way bind to the length of an Array, use ng-value instead of ng-model:
<input ng-value="tripsheets.length" type="text">
If you have a two-way binding that can be changed from the default:
$scope.tripsheet = {
tripsheet_num: $scope.tripsheets.length
};
<input ng-model="tripsheet.tripsheet_num" type="text">
Example in plunker: http://plnkr.co/edit/UM4lyGZSxB1mEcpy9CqJ?p=preview
Finally, it sounds like you may be doing a remote call to a database, so you could use the following strategy:
$scope.tripsheet = {
$resolved: false,
tripsheet_num: null
};
When the remote call succeeds, set $resolved to true and also set the value of tripsheet_num. In the HTML you could do something like this:
<input ng-disabled="!tripsheet.$resolved" ng-model="tripsheet.tripsheet_num" type="text">
Resolve example on plunker: http://plnkr.co/edit/q2Ua8KGqWr6HDphe92q0?p=preview
you are making you input a model of tripsheet.tripsheet_num which means that on the $scope there is going to be a property called tripsheet.tripsheet_num (I think, not sure because of the . what it will actually do.)
However i do know that when you make the tripsheet num a function it overrides whatever it was before and you should lose the .tripshee_num property. Your structure should be more like this.
$scope.tripsheet = {}
$scope.tripsheet.tripsheet_num = "default value";
if tripsheet is an array then change the model on you input to be "tripsheetLength" and change code to this.
<input ng-model="tripsheetLength" type="text">
$scope.tripsheets = ['info1', 'info2']
$scope.tripsheetLength = $scope.tripsheets.length; // length here is 2

how to deobfuscate javascript [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
can any one tell me how to de-obfuscate this?
É=-~-~[],ó=-~É,Ë=É<<É,þ=Ë+~[];Ì=(ó-ó)[Û=(''+{})[É+ó]+(''+{})[ó-É]+([].ó+'')[ó-É]+(!!''+'')[ó]+({}+'')[ó+ó]+(!''+'')[ó-É]+(!''+'')[É]+(''+{})[É+ó]+({}+'')[ó+ó]+(''+{})[ó-É]+(!''+'')[ó-É]][Û];Ì(Ì((!''+'')[ó-É]+(!''+'')[ó]+(!''+'')[ó-ó]+(!''+'')[É]+((!''+''))[ó-É]+([].$+'')[ó-É]+'\''+''+'\\'+(... Masked for confidentiality reasons
Look for the "()" in the end. Those are for executing the obscured function code. If you remove the last one and use "toString()" instead in node you will get the following (After formatting a bit):
function anonymous() {
na = prompt('Entrez le mot de passe');
if(a == 'I changed this to not make it too easy for you' {
alert('bravo');
} else {
alert('fail...');
}
}
Try it yourself, but always be careful, since if you are not careful this kind of code can run harmful stuff on your computer.
PS: A few more words about how it actually works. Those weird french seeming letters everywhere are just variables, which are defined in the beginning. É for example has the value of 2, since using the bitwise not operator on an empty array results a -1, and -~-(-1) = 2. All those backslashes are then used in combination with this numeric variables to get characters which eventually form the code of the function.

i am making a simple calculator(+,-,*,/ and cls) how can i get two input strings and perform operation [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
i know how to add two strings but don't know how will i obtain which no user pressed and obtain their string something to do with OnClick ?
You can get the id of the clicked button and adding it's string with the number it hab with something like that in your activity:
void methodCalledByOnClick(View v) {
switch(v.getId()) {
case R.id.btn_NrOne:
editTextWhereNumbersHaveToBeDisplayed.setText(
editTextWhereNumbersHaveToBeDisplayed.getText() + v.getText()
);
break;
case R.id.btn_NrTwo:
and so on...
}
}
To get the Number for calculation out of the textEdit please have a look at te answer from Veer Shubhranshu Shrivastav.
Also: Please try to be more precise in future questions you may have!
Do one thing, restrict the user to enter only numeric value in the Textbox.
In android EditText has a attribute inputType make it number. Then the user can only enter number. Then for thread-safe program check the ASCII value of the input between 48-54 (0-9)
Then you use
EditText ed = (EditText) findViewById(R.id.Edit1);
int num = Integer.parseInt(ed.getText); ///You got the number.
Your question did not state whether this is for an android app or a HTML app. In case you are referring to an android app then I recommend going through the Android training. That explains the basics on how to access views etc.
https://developer.android.com/training/basics/firstapp/index.html

Categories

Resources