Encoded value is displaying on textbox :- encodeURIComponent() - javascript

I'm calling a javascript function on OnClientClick of my button control to encode a textbox value .
That JS function calls encodeURIComponent() to encode the text value of textbox.
My Javascript function is
function Validate() {
var d = document.getElementById('<%=NoteText.ClientID %>');
var Hdn = document.getElementById('HdnProcessing');
if (d.value == '') {
alert(document.getElementById('<%=NotesMessage.ClientID %>').value);
d.focus();
return false;
}
if (Hdn.value == "Processing") {
return false;
} else {
Hdn.value = "Processing";
var headObj = document.getElementById('<%=NoteHeading.ClientID %>');
headObj.value = encodeURIComponent(headObj.value);
d.value = encodeURIComponent(d.value);
return true;
}
}
Here my inputs are textboxes ie NoteText and NoteHeading.
If anyone is reading this I was wondering is there anyway to pass the encoded text to the server without the user seeing the encoded text updating in the textbox. Currently when the user clicks the SAVE button they see the text change to the encoded text for a split second before the page refreshes. I was just curious if I could prevent the user from seeing that.

I recently provided an answer to a very similar question that should help you out:
JavaScript encode field without spoiling the display

Related

How to validate string with RegExp()

I have a problem that I want to validate input field using RegExp(). Allows entering digits, characters and special character "_" to get the last string: regex3_example. When pressing the space key, "_" will be displayed. Hope everybody help please. Thank you!
document.querySelector('#input').addEventListener('input', function () {
var text = this.value;
if (re.test(text)) {
console.log("Valid");
} else {
console.log("Invalid");
}
$(document).keypress(function (event) {
if (event.keyCode === 32) {
var re = new RegExp("^[a-zA-Z_]");
var insert_string = text+'_';
let insert_text = insert_string.trim();
$('#key').val(insert_string);
}
});
});
I don't use jquery, but i can help. If you don't need to process your input data but only validade for user, you can use the prop pattern in your HTML tag.
<input
type="text"
pattern="\w*_\w{1,}"
title="Try the pattern test_test"/>
In this case, when you click on Button submit, the page will prenvent this action and show in this input the message passed on the prop title.
Hope help you, see ya!

Clear the cache for a jQuery variable

I am working on sharepoint edit form, and we can define a function named PreSaveItem(), which will get executed before the form is submitted to the server, as follow:-
<script language="javascript" type="text/javascript">
function PreSaveItem(){
var result = true;
var status=$("select[id*='Status_'] option:selected").text();
if (status == "Closed") {
var analysis = $('input[id^="Analysis_"]').val().trim();
alert(analysis);
alert(Date.now());
if (analysis == "") {
alert("Please Enter Analysis before closing the item");
result = false;
}
}
return result;
}
</script>
The above script will show and alert() if the users change the status to "Closed", while they left an Input field named "Analysis" empty. but seems i am facing a caching issues when the script is reading the updated value for the $('input[id^="Analysis_"]').val().trim();. as follow:-
let say i changed the status to "Closed" + i left the "Analysis" input field empty
click on save
then i will get this alert correctly alert("Please Enter Analysis before closing the item");.
then after getting the alert, i entered some text inside the "Analysis" input field >> click on Save again.
then i will get the same error. and the alert(analysis); will still show the old empty value, while the alert(Date.now()); will show updated date-time.. so seems the var status=$("select[id*='Status_'] option:selected").text(); is being cached?
also the weird thing is that the $("select[id*='Status_'] option:selected").text() is not being cached ...
Clear value after alerting.
<script language="javascript" type="text/javascript">
function PreSaveItem(){
var result = true;
var status=$("select[id*='Status_'] option:selected").text();
if (status == "Closed") {
var analysis = $('input[id^="Analysis_"]').val().trim();
alert(analysis);
alert(Date.now());
if (analysis == "") {
alert("Please Enter Analysis before closing the item");
result = false;
var analysis = undefined;
}
}
return result;
}
</script>
Though clearly not getting what you are trying to achieve but would suggest you to update the method for selected text. It might work
suppose your select options are as follows
<select id="CountryName">
<option>India</option>
<option>Australia</option>
<option>England</option>
</select>
To get the selected text you can directly achieve selected text by:
var getSelected = $('#CountryName').find(":selected").text();

JQuery - How to check if input is comming from keyboard or not?

How can I check if input is coming from script or is inputed from keyboard to the textarea?
Is there an easy way to check that?
Im doing the form to send partly auto-generating messages where Im using drop-lists to genearte text and just pasting it to one textarea.
In have a function like:
function refresh() {
var str2 = "";
$('input[type="checkbox"]:checked').each(function() {
if (($('textarea', $(this).parent()).val()))
str2 += $('textarea', $(this).parent()).val() + " \n";
});
$('#Body').val(str2);
}
Any ideas?
You can use on keyup function like
$('#textAreaID').on('keyup',function(){
alert('its keyboard');
//do your stuff
})

Need a javascript function to identify the which textbox used while clicking save button

I have 3 textboxes in 3 tabs of same ASP.net page and corresponding 3 javascript functions of same functionality. Presently I am using 3 different functions for three textboxes. I need only a single javascript function instead of three. Please help me
<asp:TextBox ID="textBox1" runat="server" ValidationonGroup="abcd"></asp:TextBox>
<asp:CustomValidator ID="ID1" runat="server" ValidationGroup="abcd" ControlToValidate="textBox1" ErrorMessage="message" ClientValidationFunction="fname"></asp:CustomValidator>
Javascript functon
function fname(sender, args) {
var x = document.getElementById('<%=txtBox1.ClientID%>').value;
if (some condition) {
args.IsValid = false;
} else {
args.IsValid = true;
}
}
One way to do it would be checking which textbox value != "" and execute the send based on this
eg your onClick handler could contain this.
if(document.getElementById('<%=txtBox1.ClientID%>').value != ""){
do this
} else if (document.getElementById('<%=txtBox2.ClientID%>').value != ""){
do this
} else {
do this
}
Why don't you make one function that receives text box ID and call it from other 3:
function fname(textBoxID, sender, args) {
var x = document.getElementById(textBoxID).value;
if (some condition) {
args.IsValid = false;
} else {
args.IsValid = true;
}
}
When you call it for first text box, just pass '<%=txtBox1.ClientID%>' as first parameter. Same way, pass '<%=txtBox2.ClientID%>' for second text box...
There is another solution, you can find it more in detail here: Passing value from CustomValidator
In short it goes like this: use sender.id to get the id of the CustomValidator. Then so long as the validator's id is the id of the TextBox with a prefix of 'Validator_', I can use a javascript replace to get rid of the 'Validator_', therefore finding out the TextBox Id.

A potentially dangerous Request.Form value was detected from the client (ctl00$cphMain$txCourseDescription="...ace="'MS Sans Serif&#...")

My screen has a Telrik rad popup which populate on a button click(btnCourseDescription). I have taken a label to show html formated data and a textbox to work with code behind file. when I click on btnCourseDescription; Telrik's popup opens and and after submit it shows data on label and assign value to textbox using Javascript ... Here is my Javascript code...
<script type="text/javascript">
function clientShow(sender, eventArgs)
{
//$find();
var txtInput = document.getElementById("<%=lbCourseDescription.ClientID%>");
sender.argument = txtInput.innerHTML;
}
function clientClose(sender, args)
{
if (args.get_argument() != null)
{
var lbInput = document.getElementById("<%=lbCourseDescription.ClientID%>");
var txtInput = document.getElementById("<%=txCourseDescription.ClientID%>");
lbInput.innerHTML = args.get_argument();
txtInput.value = args.get_argument.htmlEncode();
}
}
</script>
Now when I click on btnCourseDescription and and assign value using popup data submits successfully but when I want to update any other field means I am not updating HTML formatted data, HARDERROR occurs:
A potentially dangerous Request.Form value was detected from the client (ctl00$cphMain$txCourseDescription="...ace="'MS Sans Serif&#...").
Please suggest me what should I do?

Categories

Resources