HTML & ASP MVC 4: error CS1056: Unexpected character '\' - asp.net-mvc

I am using asp mvc 4. I have the following html markup.
<input type="text" maxLength="2000" pattern="^(~/|https?://).*$|^mailto:([\w\.\-]+)#([\w\-]+)((\.(\w){2,3})+)$" >
I am receiving the following server error: HttpCompileException - error CS1056: Unexpected character '\'
I tried to escape it but it didn`t work. This must be simple but I am missing something. Any help will be greatly appreciated. Thanks!
Solution
The problem was in the # symbol. It should be escaped because it is used by the Razor view engine.
This works:
pattern="^(~/|https?://).*$|^mailto:([\w\.\-]+)#([\w\-]+)((\.(\w){2,3})+)$"

You may also use value of pattern's attribute as string literal:
<input type="text" maxLength="2000" pattern='#(#"^(~/|https?://).*$|^mailto:([\w\.\-]+)#([\w\-]+)((\.(\w){2,3})+)$")' >
//better:
#{
const string urlPattern = #"^(~/|https?://).*$|^mailto:([\w\.\-]+)#([\w\-]+)((\.(\w){2,3})+)$";
}
<input type="text" maxLength="2000" pattern="#urlPattern" >

Related

'+' lost from input on form submission

I have an input:
#using (Html.BeginForm("ReleaseErrors", "ManageReleases", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="hidden" name="name" value="#ViewBag.Name" />
<button type="submit" onclick="checkInput()">Add</button>
}
And I am getting the value:
string ReleaseName = Request.Form["name"];
However if it contains any '+' characters they get lost (simply missing).
I also have this problem with links:
<button onclick="location.href='/ManageReleases/Update?name=#r.Name'">Update</button>
The html contains the + characters but they don't get submitted.
How can I overcome this?
The solution I used was to format the string correctly:
r.Name.Replace("+", "%2B").Replace(" ", "%20")
This will result in the correct urls.
Assuming your checkInput just does what it says (checks input) then there's nothing immediately obvious and trying to recreate (hidden input, post, Request.Form) does not reproduce your issue - so it's possible it's somewhere else in your solution (eg web.config)
Can you create a new, simple solution to try and reproduce it?
The additional, second part:
<button onclick="location.href='/ManageReleases/Update?name=#r.Name'">Update</button>
definitely needs to be url encoded
<button onclick="location.href='/ManageReleases/Update?name=#HttpUtility.UrlEncode(r.Name)'">Update</button>

How to not show single quote in id attribute

I'm using MVC3 Razor view.
I have following code
#{
var count =0;
<input type="text" id="name'#count'" />
// Some more html elements
}
Now, when i view this html in browser the ID of text element is showing id="name'0'"
How to not show the single quotes in id attribute.
Like this
id="name0"
Please help me with this..
This should solve your problem:
#{
var count= 0;
<input type="text" id="name#{#count}" />
}
Try some thing like this
<input type="text" id="Name#(#count)" />
if you are doing it in a javascript file ,while defining you id make use of '+' operator
Ex: id="name"+"#count"

Copy and paste url parts in forms

How can I get a part of a string from the specific starting position to end. Like for example I want to copy v=mQUr2RkjykU from the given url:
http://www.youtube.com/watch?v=mQUr2RkjykU
If url is writen into form, how can i copy and paste that part.
And write it mysite.com/watch?xxxxxxxxxxx in a link thats generated.
<input id="Form" name="Form" type="text" /><input id="Button" type="button" value="" />
Thank you
You can use substr() and strpos()
In your case , you can check the position of '?' in your URL>>
$oldUrl = 'http://www.youtube.com/watch?v=mQUr2RkjykU';
$newUrl = substr($oldUrl, 1, strpos($oldUrl, '?')-1);
$newUrl would be your answer,
anyway , try to search more before asking question here :)

Syntax error with parseJSON during unobtrusive validation

My MVC app is generating the following HTML which causes a Javascript syntax error upon submission (I'm not typing anything into the two text boxes). Here's the generated HTML and the submit handler:
<form action="/UrIntake/Save" id="UrIntakeForm" method="post">
<input data-val="true" data-val-length="The field LastName must be a string with a maximum length of 50." data-val-length-max="50" data-val-required="The LastName field is required." id="FormSubmitter_LastName" name="FormSubmitter.LastName" type="text" value="" />
<input data-val="true" data-val-length="The field FirstName must be a string with a maximum length of 50." data-val-length-max="50" data-val-required="The FirstName field is required." id="FormSubmitter_FirstName" name="FormSubmitter.FirstName" type="text" value="" />
<div id="SubmissionButtons" class="right">
<input type="button" onclick="SubmitForm()" value="Submit" />
<input type="button" onclick="CancelForm()" value="Cancel" />
</div>
</form>
function SubmitForm() {
$("#UrIntakeForm").valid();
.
.
.
This is the jQuery code where the syntax error is occurring (v1.9.0). "data" is undefined and the "return" line is where the error occurs:
parseJSON: function( data ) {
// Attempt to parse using the native JSON parser first
if ( window.JSON && window.JSON.parse ) {
return window.JSON.parse( data );
}
Presumably, I don't have to enter anything into the text boxes (and should then get the "field is required" message). Is this what's causing the error? That doesn't make sense, but I don't see what else it could be.
Cause
This is an issue with jquery.validate.unobtrusive.js in your ASP.NET.MVC package.
As of jQuery 1.9, the behavior of parseJSON() has changed and an undefined value would be considered a malformed JSON, resulting in the error you've specified. See the jQuery 1.9 Core Upgrade Guide for more information.
Solution
Use the jQuery Migrate plugin, which among other things adds backward-compatibility to the jQuery parseJSON() utility.
EDIT
According to the official announcement in this thread on Microsoft Connect, the issue has been resolved in the latest release of the framework.
Naturally, as Andreas Larsen noted in the comments, make sure to clear any relevant cache, server-side and client-side, after upgrading to the new release.
I also had this issue. The problem was that $.parseJSON(undefined) causes an exception to be thrown, and that the unobtrusive validation was making that call. As stated in the accepted answer, this has since been fixed.
You can download the Microsoft version of this script which will properly validate without causing an exception from this link: http://ajax.aspnetcdn.com/ajax/mvc/5.1/jquery.validate.unobtrusive.min.js

JSLint message: "expecting expecting <\/ instead of <\ " -What is the reason(s) for that error?

I was using JSLint to validate my code and i received the following error:
"expecting </ instead of <\"
I got that for my HTML code and JavaScript code. For example:
element.innerHTML += "<p>here's what happened: You are <b>" + aVariable + " </b> years old.</p>";
and:
<input type="text" name="enterText" id="nameOfPerson" onblur="submitValues(this)" size="50" value="Enter your name"/>
Can anyone tell me why this is happening?
In the JSLint section on HTML, it says:
JSLint also checks for the occurrence of '</' in string literals. You should always
write '</' instead. The extra backslash is ignored by the JavaScript compiler but
not by the HTML parser. Tricks like this should not be necessary, and yet they are.

Resources