Value of Mobile Number entered showing "not a valid number" despite putting in regex for 10 digits - regex

I am trying to put in validation for the mobile number entered by the user despite putting in regular expression for 10 digit mobile number.
Here is the Model class
public partial class student1
{
public int StudentId { get; set; }
[Required]
[StringLength(30)]
public string Name { get; set; }
public string Branch { get; set; }
[Display(Name = "Mobile Number:")]
[Required(ErrorMessage = "Mobile Number is required.")]
[RegularExpression("^([07][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9] | 8[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9] | 9[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9])", ErrorMessage = "Invalid Mobile Number.")]
public Nullable<int> Mobile { get; set; }
}
Create view
#Html.EditorFor(model => model.Mobile)
#Html.ValidationMessageFor(model => model.Mobile, "", new { #class = "text-danger" })
When I run it, for mobile number entered less than 10 it shows the error message that I had written. But for value 10 and greater I get a new message that says
The value '999999999' is not valid for Mobile Number:.
I don't know where is this message coming from. Also, why isn't it accepting the 10 digit value?

Maximum value for int in c# for 64 bit machine is
2,147,483,647
which is less than 9999999999 so it is overflow for int, so it is error message for model validation from Controller before model binding.
Convert int to long
public Nullable<long> Mobile { get; set; }
Also check regular expression, it might miss some valid numbers too.
int max value for c# source:
https://msdn.microsoft.com/en-us/library/system.int32.maxvalue(v=vs.110).aspx
What is the int.MaxValue on a 64-bit PC?

try Regular Expression = ^(\d{1,3}[- ]?)?\d{10}$

Related

DataAnnotation for Date

I got a problem using DataAnnotations, here is my code:
[Display(Name = "Admission date")]
[DataType(DataType.Date, ErrorMessage = "The field {0} is not in the correct format")]
[Required(ErrorMessage = "The {0} field is required.")]
[Date(ErrorMessage = "Date for {0} must be between {1} and {2}")]
[RegularExpression(#"^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]|(?:Jan|Mar|May|Jul|Aug|Oct|Dec)))\1|(?:(?:29|30)(\/|-|\.)(?:0?[1,3-9]|1[0-2]|(?:Jan|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec))\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)(?:0?2|(?:Feb))\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9]|(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep))|(?:1[0-2]|(?:Oct|Nov|Dec)))\4(?:(?:1[6-9]|[2-9]\d)\d{2})$", ErrorMessage = "The field {0} is not in the correct format.")]
public DateTime AdmissionDate { get; set; }
It works perfectly (IF I COMENT this below code):
[Display(Name = "Leadership date")]
[DataType(DataType.Date, ErrorMessage = "The field {0} is not in the correct format")]
[Date(ErrorMessage = "Date for {0} must be between {1} and {2}")]
[RegularExpression(#"^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]|(?:Jan|Mar|May|Jul|Aug|Oct|Dec)))\1|(?:(?:29|30)(\/|-|\.)(?:0?[1,3-9]|1[0-2]|(?:Jan|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec))\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)(?:0?2|(?:Feb))\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9]|(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep))|(?:1[0-2]|(?:Oct|Nov|Dec)))\4(?:(?:1[6-9]|[2-9]\d)\d{2})$", ErrorMessage = "The field {0} must be a date.")]
public Nullable<DateTime> HeadShipDate { get; set; }
I don't know why, but it really happens, I commented the Regex validation for HeadShipDate, and AdmissionDate validation works, if I uncomment the that validation for HeadShipDate, it seems confused and match any date.
Btw, is the same Regex.
Your regex for HeadShipDate needs to be within a string and begin with the # sign, like the first one. What you posted won't compile.

Regex get group n from group results

I have a result from a database query that is:
IEnumerable<ResultRecord> results_from_db_call = Db.GoGetItNow();
Now lets assume the returned class looks like:
public class ResultsRecord
{
public string DataBlob { get; set; }
public int FirstID { get; set; }
public int SecondNum { get; set; }
public DateTime ThirdDate { get; set; }
public string FourthTime { get; set; }
public string FifthTime { get; set; }
}
Now, the result records returned only have the DataBlob member set
It very well might look like:
<IK/12322>1354654 16/MAY/2014 18:01:01 - 20:01:01
So, I need to fill the other members, and would like to do so with an 'Each' delegate:
results.Each(f => f.FirstID = int.Parse(Regex.Match(f.DataBlob, #"\d+").Value));
results.Each(f => f.SecondNum = int.Parse(Regex.Match(f.DataBlob, #"\d+").Value));
results.Each(f => f.ThirdDate = DateTime.Parse(Regex.Match(f.DataBlob, #"\d+").Value));
results.Each(f => f.FourthTime = Regex.Match(f.DataBlob, #"\d+").Value));
results.Each(f => f.FifthTime = Regex.Match(f.DataBlob, #"\d+").Value));
Well, as you can imagine, that regex expression only worked on the first integer..
But wait! I have a working Regex to find all the groups I need:
(\d+)\>(\d+) (\d+\/[a-zA-Z]+\/\d+) (\d+\:\d+\:\d+) - (\d+\:\d+\:\d+)
This successfully groups all items that I need.
But, and here's the question, how do I get the second regex group item for SecondNum, the third regex group item for ThirdDate, the fourth regex group item for FourthTime, and the fifth regex group item for FifthTime?
When I try
(\d+)\>(\d+) (\d+\/[a-zA-Z]+\/\d+) (\d+\:\d+\:\d+) - (\d+\:\d+\:\d+){0}
I don't get the first (zeroth) item.
How can I call the regex to get the first, etc item from the groupings?
results.Each(f => f.SecondNum = int.Parse(Regex.Match(f.DataBlob, #"????").Value));
You should parse the whole string first so that you will be able to capture different groups:
string pattern = #"(\d+)\>(\d+) (\d+\/[a-zA-Z]+\/\d+) (\d+\:\d+\:\d+) - (\d+\:\d+\:\d+)";
Match m = Regex.Match(f.DataBlob, pattern);
// You can then refer to the matched group this way:
// m.Groups[1] = 12322
// m.Groups[2] = 1354654
// ...etc
Group g = m.Groups[2];
// Then parse into integer
Do note that capture group starts from 1, instead of 0. You can also take a look at the example here.

PETAPOCO - Invalid object name

I am using CTE with PetaPOCO and getting a strange error
SQL Exception: Invalid Object Name PayTransactionForRollingVacationAverage that references the model that the data should map to.
The code is as follows.
public IEnumerable<PayTransactionForRollingVacationAverage> GetPayTransactionForRollingVacationAverage(DateTime payEndingDate)
{
PointsNorth.PetaPoco.Sql sql = new PointsNorth.PetaPoco.Sql();
sql.Append(#"
;with HolidayWeeks as
(
Select Distinct EmployeeId, PayEndDate, 'Y' as HolidayWeek
from PayTransactions
where PayEndDate = #payEndingDate
and LaborCode in ('251', '249')
)", new { payEndingDate });
sql.Append(#"
Select
PT.EmployeeId,
PT.PayEndDate,
J.JobClass,
PayCodes.AverageRateCode,
PT.RegularHours,
PT.RegularRate,
PT.RegularAmount
from PayTransactions PT
Left Outer Join PayCodes on PayCodes.PayCodeCode = PT.LaborCode
Left Outer Join HolidayWeeks as H on H.PayEndDate = PT.PayEndDate and H.EmployeeId = PT.EmployeeId
Inner Join Jobs as J on J.JobId = PT.JobId
where PT.PayEndDate = #payEndingDate
and IsNull(H.HolidayWeek, 'N') <> 'Y'
order by PT.EmployeeId, PT.PayEndDate, J.JobClass", new { payEndingDate });
var data = Database.Query<PayTransactionForRollingVacationAverage>(sql);
return data;
}
The model is pretty simple:
public class PayTransactionForRollingVacationAverage
{
public long EmployeeId { get; set; }
public DateTime PayEndDate { get; set; }
public string JobClass { get; set; }
public string AverageRateCode { get; set; }
public decimal RegularHours { get; set; }
public decimal RegularRate { get; set; }
public decimal RegularAmount { get; set; }
}
I tried breaking the SQL up to make sure it was building correctly, but I still get the error. Any idea why this is occurring?
Remove the whitespace before the semi-colon.
According to Alex Jorgenson's link, this is fixed if you make a semi-colon the very first character.
According to my testing, this is strictly the first character, i.e. if there is even some whitespace before the semi-colon, the auto-generated code will still be spit out.
This is a known issue with Peta Poco. It appears to be fixed in some version but I don't know which one. You can find more information and a work around for this particular issue at https://github.com/toptensoftware/PetaPoco/issues/22

MVC3 Regular Expressions Designer.cs

Taking the database first approach in MVC3 all of my models are created and stored in a designer.cs
In the code below, I want to force a regex validation of the NDC property. The input needs to resemble 1234-1234-12 or 4 digits a dash 4 digits a dash 2 digits.
public partial class Drug : EntityObject
{
#region Factory Method
[EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)]
[DataMemberAttribute()]
public global::System.String NDC
{
[Required(ErrorMessage = "Please enter the Rx NDC")]
[RegularExpression(#"\d\d\d\d-\d\d\d\d-\d\d", ErrorMessage = "Please enter a correctly formatted NDC")]
get
{
return _NDC;
}
set
{
if (_NDC != value)
{
OnNDCChanging(value);
ReportPropertyChanging("NDC");
_NDC = StructuralObject.SetValidValue(value, false);
ReportPropertyChanged("NDC");
OnNDCChanged();
}
}
}I dont know how to apply the code above in the code below because I get the this error:
Error 13 Attribute 'Required' is not valid on this declaration type. It is only valid on 'property, indexer, field, param' declarations. C:\Users\Daniel\Desktop\320Final -Updated\320Final\Models\DBModel.Designer.cs
You're trying to set the attributes inside of the property:
public global::System.String NDC
{
[Required(ErrorMessage = "Please enter the Rx NDC")]
[RegularExpression(#"\d\d\d\d-\d\d\d\d-\d\d", ErrorMessage = "Please enter a correctly formatted NDC")]
get
{
return _NDC;
}
...
You need to set them on the property itself:
[Required(ErrorMessage = "Please enter the Rx NDC")]
[RegularExpression(#"\d\d\d\d-\d\d\d\d-\d\d", ErrorMessage = "Please enter a correctly formatted NDC")]
public global::System.String NDC
{
get
{
return _NDC;
}
....

Why does my XML ASP.NET web service return results which repeats itself?

I have written an ASP.NET web service.
It looks like this:
WebServices.logic pLogic = new WebServices.logic();
WebServices.manager[] pManager = new PowerManager[1];
pManager[0] = new PowerManager();
pManager[0].CustomerId = "sjsjshd";
pManager[0].state = pLogic.getState("sasj");
return pManager[0];
The pManager class looks like this:
public string _CustomerId;
public int PowerStatus;
public List<ArrayList> _Power;
public string CustomerId
{
get
{
return _CustomerId;
}
set
{
_CustomerId = value;
}
}
public List<ArrayList> Power
{
get
{
return _Power;
}
set
{
_Power = value;
}
}
When I run it, I get a repetition of the results, like so:
<p>
<_CustomerId>sjsjshd</_CustomerId>
<pStatus>0</PowerStatus>
−
<_p>
−
<ArrayOfAnyType>
<anyType xsi:type="xsd:int">1</anyType>
</ArrayOfAnyType>
<ArrayOfAnyType/>
</_p>
<CustomerId>sjsjshd</CustomerId>
−
<p>
−
<ArrayOfAnyType>
<anyType xsi:type="xsd:int">1</anyType>
</ArrayOfAnyType>
<ArrayOfAnyType/>
</p>
</pManager>
However, there is no duplicate values stored (Eg. I store client name in a collection, but only once - count of 1). There are no duplicates stored when I call getState(). This method returns a collection and it contains one value, but the results in XML has a repetition of this.
How comes the results appear to repeat themselves? When running the system, I only get one error.
Thanks
OK, looks like your XML serialization is giving you all the public members of your PowerManager class. Based on the naming convention of starting with an underscore, those members should be private, like this:
private string _CustomerId;
private List<ArrayList> _Power;
You also state "When running the system, I only get one error." What error are you getting?